C# 按字符串为Func指定方法名

C# 按字符串为Func指定方法名,c#,asp.net,c#-4.0,C#,Asp.net,C# 4.0,基本上我想这样做: Func<string> f = ()=> MyMethodName(); Func f=()=>MyMethodName(); 仅具有方法的字符串名称,即: Func<string> f = "MyMethodName"; Func f=“MyMethodName”; 这能做到吗?有什么问题吗?反思能帮上忙吗?我可以先检查方法是否存在吗?下面是一个反射示例: Func<string> func = ( ) =>

基本上我想这样做:

Func<string> f =  ()=> MyMethodName();
Func f=()=>MyMethodName();
仅具有方法的字符串名称,即:

 Func<string> f =  "MyMethodName";
Func f=“MyMethodName”;

这能做到吗?有什么问题吗?反思能帮上忙吗?我可以先检查方法是否存在吗?

下面是一个反射示例:

Func<string> func = ( ) => {
    return ( string )( this.GetType( ).GetMethod( "MyMethodName" ).Invoke( this, new object[ 0 ] ) );
}
Func-Func=()=>{
return(string)(this.GetType().GetMethod(“MyMethodName”).Invoke(this,新对象[0]);
}
如果你想让事情变得轻松,这里有一些:

public static Func<string> ReflectByName( object obj, string methodname ) {
    return ( ) => {
        return ( string )( obj.GetType( ).GetMethod( methodname ).Invoke( obj, new object[ 0 ] ) );
    }
}
public static Func ReflectByName(对象对象对象,字符串方法名){
return()=>{
return(string)(obj.GetType().GetMethod(methodname).Invoke(obj,新对象[0]);
}
}
如何使用:

Func<string> f = FastReflect.ReflectByName( this, "MyMethodName" );
Func f=FastReflect.ReflectByName(这是“MyMethodName”);

FastReflect
方法
ReflectByName
在哪里。

下面是一个反射示例:

Func<string> func = ( ) => {
    return ( string )( this.GetType( ).GetMethod( "MyMethodName" ).Invoke( this, new object[ 0 ] ) );
}
Func-Func=()=>{
return(string)(this.GetType().GetMethod(“MyMethodName”).Invoke(this,新对象[0]);
}
如果你想让事情变得轻松,这里有一些:

public static Func<string> ReflectByName( object obj, string methodname ) {
    return ( ) => {
        return ( string )( obj.GetType( ).GetMethod( methodname ).Invoke( obj, new object[ 0 ] ) );
    }
}
public static Func ReflectByName(对象对象对象,字符串方法名){
return()=>{
return(string)(obj.GetType().GetMethod(methodname).Invoke(obj,新对象[0]);
}
}
如何使用:

Func<string> f = FastReflect.ReflectByName( this, "MyMethodName" );
Func f=FastReflect.ReflectByName(这是“MyMethodName”);

fastreef
方法
ReflectByName
在哪里。

这里根本不需要lambda表达式。您可以使用:

MethodInfo-method=GetType().GetMethod(methodName);
Func Func=(Func)Delegate.CreateDelegate(typeof(Func),
obj,方法);

这样就避免了一定程度的间接性,而且反射部分也只执行一次而不是每次调用。

这里根本不需要lambda表达式。您可以使用:

MethodInfo-method=GetType().GetMethod(methodName);
Func Func=(Func)Delegate.CreateDelegate(typeof(Func),
obj,方法);

这样可以避免一定程度的间接性,而且反射部分也可以执行一次,而不是每次调用都执行一次。它的优点是比反射速度快得多,所以如果需要在循环中调用它,这是非常好的。这段代码创建一个Func来调用您的方法

    public static Func<T, string> CreateCallFunc<T>(string methodName)
    {
        var parm = Expression.Parameter(typeof(T));
        var call = Expression.Call(parm, typeof(T).GetMethod(methodName));
        var lambda = Expression.Lambda<Func<T, string>>(call, parm);
        return (Func<T, string>)lambda.Compile();
    }
public static Func CreateCallFunc(string methodName)
{
var parm=表达式参数(typeof(T));
var call=Expression.call(parm,typeof(T).GetMethod(methodName));
var lambda=Expression.lambda(call,parm);
返回(Func)lambda.Compile();
}
您可以这样使用它:

    class SomeClass
    {
        void DoIt()
        {
            var func = CreateCallFunc<SomeClass>("SomeMethod");
            Console.WriteLine(func(this));
        }

        public string SomeMethod()
        {
            return "it works!";
        }
    }
class-SomeClass
{
void DoIt()
{
var func=CreateCallFunc(“SomeMethod”);
Console.WriteLine(func(this));
}
公共字符串方法()
{
返回“它工作!”;
}
}
最大的优点是,一旦创建了Func,只需使用Func(myInstance)就可以非常简单地调用它,而且速度非常快

我使用此方法获取DataReader,并将读取的所有数据复制到类集合中,其中类上的属性名称与读取器中的字段匹配


请注意,John的答案是一种更简单的方法,我直到现在才意识到这一点,但它具有更大的灵活性,我想这里是另一种方法。它的优点是比反射速度快得多,所以如果需要在循环中调用它,这是非常好的。这段代码创建一个Func来调用您的方法

    public static Func<T, string> CreateCallFunc<T>(string methodName)
    {
        var parm = Expression.Parameter(typeof(T));
        var call = Expression.Call(parm, typeof(T).GetMethod(methodName));
        var lambda = Expression.Lambda<Func<T, string>>(call, parm);
        return (Func<T, string>)lambda.Compile();
    }
public static Func CreateCallFunc(string methodName)
{
var parm=表达式参数(typeof(T));
var call=Expression.call(parm,typeof(T).GetMethod(methodName));
var lambda=Expression.lambda(call,parm);
返回(Func)lambda.Compile();
}
您可以这样使用它:

    class SomeClass
    {
        void DoIt()
        {
            var func = CreateCallFunc<SomeClass>("SomeMethod");
            Console.WriteLine(func(this));
        }

        public string SomeMethod()
        {
            return "it works!";
        }
    }
class-SomeClass
{
void DoIt()
{
var func=CreateCallFunc(“SomeMethod”);
Console.WriteLine(func(this));
}
公共字符串方法()
{
返回“它工作!”;
}
}
最大的优点是,一旦创建了Func,只需使用Func(myInstance)就可以非常简单地调用它,而且速度非常快

我使用此方法获取DataReader,并将读取的所有数据复制到类集合中,其中类上的属性名称与读取器中的字段匹配


请注意,John的答案是一种更简单的方法,我直到现在才意识到这一点,但这具有更大的灵活性,我想

您是否希望始终将
MyMethodName
作为当前类的静态或实例成员,或者您是否需要调用另一个对象的方法?@hvd,它可能是另一个对象的方法。目前我的方法是这样的:私有字符串MyMethodName(){return new List();},然后它比目前为止答案中的
GetMethod(“MyMethodName”)
复杂得多。或者您可以直接传递对象的实例吗?如果在编译时知道
“MyMethodName”
,您可能更喜欢使用
动态
关键字。例如
dynamic d=myObj;Func f=()=>d.MyMethodName()
是否始终将
MyMethodName
作为当前类的静态或实例成员查找,或者是否需要调用另一个对象的方法?@hvd,它可能是另一个对象的方法。目前我的方法看起来是这样的:私有字符串MyMethodName(){return new List();}然后它比您得到的
GetMethod(“MyMethodName”)
复杂得多