Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将类方法名称作为lamba表达式传递_C#_Reflection_Interface_Lambda_Delegates - Fatal编程技术网

C# 将类方法名称作为lamba表达式传递

C# 将类方法名称作为lamba表达式传递,c#,reflection,interface,lambda,delegates,C#,Reflection,Interface,Lambda,Delegates,我正在为一些api调用编写一个插件框架,这些api调用针对具有各种安全需求的各种客户机,以收集各种特定于业务的数据。所有插件都实现了iapiserviceinterypoint,如下所示: public interface IApiServiceEntryPoint : IDisposable { /// <summary> /// Gets the name of the API Plugin /// </summary> string

我正在为一些api调用编写一个插件框架,这些api调用针对具有各种安全需求的各种客户机,以收集各种特定于业务的数据。所有插件都实现了
iapiserviceinterypoint
,如下所示:

public interface IApiServiceEntryPoint : IDisposable
{
    /// <summary>
    /// Gets the name of the API Plugin
    /// </summary>
    string Name { get; }

    /// <summary>
    /// Registers the assembly in the application, sets up the routes, and enables invocation of API requests
    /// </summary>
    void Register();

    /// <summary>
    /// Gets the routing namespace of the plugin
    /// </summary>
    string UrlNameSpace { get; }

    /// <summary>
    /// Validates the user is authorized to invoke the supplied method.
    /// </summary>
    /// <param name="methodName"></param>
    bool IsAuthorized(string methodName);

    /// <summary>
    /// The user initiating the API call
    /// </summary>
    IPrincipal User { get; }
}
   [HttpGet]
    public DateTime GetSystemTimeStamp()
    {
        if (IsAuthorized(me => me.GetSystemTimeStamp))
        {
            return DateTime.UtcNow;
        }
        throw new AuthorizationException();
    }
我想做的是这样的:

public interface IApiServiceEntryPoint : IDisposable
{
    /// <summary>
    /// Gets the name of the API Plugin
    /// </summary>
    string Name { get; }

    /// <summary>
    /// Registers the assembly in the application, sets up the routes, and enables invocation of API requests
    /// </summary>
    void Register();

    /// <summary>
    /// Gets the routing namespace of the plugin
    /// </summary>
    string UrlNameSpace { get; }

    /// <summary>
    /// Validates the user is authorized to invoke the supplied method.
    /// </summary>
    /// <param name="methodName"></param>
    bool IsAuthorized(string methodName);

    /// <summary>
    /// The user initiating the API call
    /// </summary>
    IPrincipal User { get; }
}
   [HttpGet]
    public DateTime GetSystemTimeStamp()
    {
        if (IsAuthorized(me => me.GetSystemTimeStamp))
        {
            return DateTime.UtcNow;
        }
        throw new AuthorizationException();
    }

我将如何在我的界面中声明,以及如何在
IsAuthorized
方法中提取方法名称以检查授权?

您不需要lambda;您可以直接将该方法作为委托传递:

public bool IsAuthorized<T>(Func<T> method) {
    string name = method.Method.Name;
}
if (IsAuthorized(GetSystemTimeStamp))
public bool已授权(Func方法){
字符串名称=method.method.name;
}
如果(未授权(GetSystemTimeStamp))
对于要接受的每种方法,需要单独的
Func
重载;它们都可以简单地调用一个接受委托的公共方法
或者,您可以只创建一个接受委托的方法,然后在每个调用站点显式地创建一个委托。

您不需要lambda;您可以直接将该方法作为委托传递:

public bool IsAuthorized<T>(Func<T> method) {
    string name = method.Method.Name;
}
if (IsAuthorized(GetSystemTimeStamp))
public bool已授权(Func方法){
字符串名称=method.method.name;
}
如果(未授权(GetSystemTimeStamp))
对于要接受的每种方法,需要单独的
Func
重载;它们都可以简单地调用一个接受委托的公共方法
或者,您可以只创建一个接受委托的方法,然后在每个调用站点显式创建一个委托。

当然,现在如果有人传入lambda,它会发出叫声。@Servy:它不会发出叫声;它将返回一个奇怪的编译器生成的名称。您可以通过查找
CompilerGeneratedAttribute
来检查这一点。然后发生的最坏情况是
已授权
检查默认为false:PYeah,但这不是OP要查找的,因为它是编译的,而不是
表达式
,没有真正好的方法可以在匿名方法内部调用该方法。因此,即使它在编译和运行时不会抛出异常,它也不会工作。我不是说这不是一个好的解决方案,因为它是,我只是明确地说,如果你要使用它,你需要明确地不使用lambda。让它与lambdas一起工作需要额外的努力。太棒了。我想得太多了。谢谢当然,现在如果有人真的经过一只羔羊,它会叫。@Servy:它不会叫;它将返回一个奇怪的编译器生成的名称。您可以通过查找
CompilerGeneratedAttribute
来检查这一点。然后发生的最坏情况是
已授权
检查默认为false:PYeah,但这不是OP要查找的,因为它是编译的,而不是
表达式
,没有真正好的方法可以在匿名方法内部调用该方法。因此,即使它在编译和运行时不会抛出异常,它也不会工作。我不是说这不是一个好的解决方案,因为它是,我只是明确地说,如果你要使用它,你需要明确地不使用lambda。让它与lambdas一起工作需要额外的努力。太棒了。我想得太多了。谢谢