Skip to content

Instantly share code, notes, and snippets.

@ylhyh
Created April 25, 2018 19:08
Show Gist options
  • Select an option

  • Save ylhyh/679179d994376bcde7008ef500bca32f to your computer and use it in GitHub Desktop.

Select an option

Save ylhyh/679179d994376bcde7008ef500bca32f to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace ReflectCallGenericMethod
{
class Program
{
static void Main(string[] args)
{
Assembly assembly = Assembly.GetExecutingAssembly();
Type typeService = assembly.GetTypes()
.Where(t => t.IsClass && t.Name == "ExampleService").SingleOrDefault();
Type typeEntity = assembly.GetTypes()
.Where(t => t.IsClass && t.Name == "ExampleEntity").SingleOrDefault();
ParameterExpression paramExp = Expression.Parameter(typeEntity);
Expression expression = null;
Type[] types = Type.EmptyTypes;
object[] parameters = null;
var condition = Console.ReadLine();
if (condition.Length > 0)//如果需要过滤数据
{
Expression propExp = Expression.Property(paramExp, "ID");
Expression constExp = Expression.Constant(3);
expression = Expression.Equal(propExp, constExp);
Type delegateType = typeof(Func<,>).MakeGenericType(typeEntity, typeof(bool));
LambdaExpression lambda = Expression.Lambda(delegateType, expression, paramExp);
types = new[] { lambda.GetType() };
parameters = new[] { lambda };
}
MethodInfo methodGetInstance = typeService.GetMethod("GetInstance");
MethodInfo methodGetData = typeService.GetMethod("GetData", types);
var instanceService = methodGetInstance.Invoke(null, null);
string result = methodGetData.Invoke(instanceService, parameters) as string;
Console.WriteLine(result);
Console.ReadLine();
//https://blogs.msdn.microsoft.com/yirutang/2005/09/14/getmethod-limitation-regarding-generics/
var genericMethods = typeService.GetMember("GetGenericData");
}
}
public class ExampleService
{
private static readonly Object _mutex = new Object();
volatile static ExampleService _instance;
public static ExampleService GetInstance()
{
if (_instance == null)
{
lock (_mutex)
{
if (_instance == null)
{
_instance = new ExampleService();
}
}
}
return _instance;
}
public string GetData()
{
return "无参方法被调用";
}
public string GetData(Expression<Func<ExampleEntity, bool>> lambda)
{
return "有参方法被调用";
}
public string GetGenericData<ExampleEntity>()
{
return "无参泛型方法被调用";
}
public string GetGenericData<ExampleEntity>(Expression<Func<ExampleEntity, bool>> lambda)
{
return "有参泛型方法被调用";
}
}
public class ExampleEntity
{
public int ID { get; set; }
public string Name { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment