C# 使用反射获取具有特定属性值的方法

C# 使用反射获取具有特定属性值的方法,c#,linq,C#,Linq,我有一位代表: public delegate void PacketHandler(Client client, Packet packet); 我有这个属性: [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] public sealed class PacketHandlerAttribute : Attribute { public ServerType Server { get; private s

我有一位代表:

 public delegate void PacketHandler(Client client, Packet packet);
我有这个属性:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public sealed class PacketHandlerAttribute : Attribute
{
    public ServerType Server { get; private set; }
    public int Code { get; private set; }

    public PacketHandlerAttribute(ServerType server, int code)
    {
        this.Server = server;
        this.Code = code;
    }
}
我想创建一个方法,根据给定的
ServerType
值返回
IEnumerable
。因此,如果我使用
ServerType.Server
调用此方法,它将返回所有包含
Server1
的方法作为其属性

到目前为止,我已经做到了:

public static IEnumerable<Doublet<PacketHandlerAttribute, PacketHandler>> GetPacketHandlers(ServerType server)
    {
        foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
        {
            foreach (MethodInfo method in type.GetMethods())
            {
                PacketHandlerAttribute attribute = (PacketHandlerAttribute)method.GetCustomAttribute(typeof(PacketHandlerAttribute));

                if (attribute != null)
                {
                    if (attribute.Server == server)
                    {
                        yield return new Doublet<PacketHandlerAttribute, PacketHandler>(attribute, (PacketHandler)Delegate.CreateDelegate(typeof(PacketHandler), method));
                    }
                }
            }
        }
    }
公共静态IEnumerable GetPacketHandler(服务器类型服务器)
{
foreach(在Assembly.getExecutionGassembly().GetTypes()中键入类型)
{
foreach(类型为.GetMethods()的MethodInfo方法)
{
PacketHandlerAttribute属性=(PacketHandlerAttribute)方法.GetCustomAttribute(typeof(PacketHandlerAttribute));
if(属性!=null)
{
if(attribute.Server==Server)
{
返回新的Doublet(属性,(PacketHandler)Delegate.CreateDelegate(typeof(PacketHandler),方法));
}
}
}
}
}

我想问一下这样做是否正确,以及如何使用LINQ缩短这段时间?

我认为这会有所帮助

 var methods = from type in assembly.GetTypes()
                      from method in type.GetMethods()
                      let attribute = method.GetCustomAttributes(typeof(PacketHandlerAttribute), false).Cast<PacketHandlerAttribute>().FirstOrDefault()
                      where attribute?.Code == 1
                      select new { method, attribute };
var methods=来自程序集中的类型。GetTypes()
来自类型为.GetMethods()的方法
let attribute=method.GetCustomAttributes(typeof(PacketHandlerAttribute),false).Cast().FirstOrDefault()
where属性?.Code==1
选择新的{方法,属性};
你的方法

public IEnumerable<(PacketHandlerAttribute attribute, PacketHandler handler)> GetPacketHandlers(ServerType server)
        {
            var assembly = Assembly.GetEntryAssembly();

            return from type in assembly.GetTypes()
                          from method in type.GetMethods()
                          let attribute = method.GetCustomAttributes(typeof(PacketHandlerAttribute), false).Cast<PacketHandlerAttribute>().FirstOrDefault()
                          where attribute?.Server == server
                          select (attribute, (PacketHandler)method.CreateDelegate(typeof(PacketHandler)));
        }
public IEnumerable GetPacketHandler(服务器类型服务器)
{
var assembly=assembly.GetEntryAssembly();
从程序集中的类型返回。GetTypes()
来自类型为.GetMethods()的方法
let attribute=method.GetCustomAttributes(typeof(PacketHandlerAttribute),false).Cast().FirstOrDefault()
where属性?.Server==服务器
选择(attribute,(PacketHandler)method.CreateDelegate(typeof(PacketHandler));
}

以下是一些我认为会有所帮助的东西

 var methods = from type in assembly.GetTypes()
                      from method in type.GetMethods()
                      let attribute = method.GetCustomAttributes(typeof(PacketHandlerAttribute), false).Cast<PacketHandlerAttribute>().FirstOrDefault()
                      where attribute?.Code == 1
                      select new { method, attribute };
var methods=来自程序集中的类型。GetTypes()
来自类型为.GetMethods()的方法
let attribute=method.GetCustomAttributes(typeof(PacketHandlerAttribute),false).Cast().FirstOrDefault()
where属性?.Code==1
选择新的{方法,属性};
你的方法

public IEnumerable<(PacketHandlerAttribute attribute, PacketHandler handler)> GetPacketHandlers(ServerType server)
        {
            var assembly = Assembly.GetEntryAssembly();

            return from type in assembly.GetTypes()
                          from method in type.GetMethods()
                          let attribute = method.GetCustomAttributes(typeof(PacketHandlerAttribute), false).Cast<PacketHandlerAttribute>().FirstOrDefault()
                          where attribute?.Server == server
                          select (attribute, (PacketHandler)method.CreateDelegate(typeof(PacketHandler)));
        }
public IEnumerable GetPacketHandler(服务器类型服务器)
{
var assembly=assembly.GetEntryAssembly();
从程序集中的类型返回。GetTypes()
来自类型为.GetMethods()的方法
let attribute=method.GetCustomAttributes(typeof(PacketHandlerAttribute),false).Cast().FirstOrDefault()
where属性?.Server==服务器
选择(attribute,(PacketHandler)method.CreateDelegate(typeof(PacketHandler));
}

您在哪里使用
服务器
参数?不清楚你想要什么。@Kyle我不清楚,因为我不知道如何检查。我知道如何检查方法是否包含该属性,但我不确定如何检查
server
参数。您在哪里使用
server
参数?不清楚你想要什么。@Kyle我不清楚,因为我不知道如何检查。我知道如何检查方法是否包含该属性,但我不确定如何检查
server
参数。谢谢!“我真的需要学习如何使用linq。”吉尔伯特威廉姆斯很高兴我能帮上忙。还重写了您的方法,并在元组方法中添加了一些c#7。谢谢!“我真的需要学习如何使用linq。”吉尔伯特威廉姆斯很高兴我能帮上忙。还重写了您的方法,并在元组方法中添加了一些c#7。