解析类与自定义属性和方法添加到委托列表

问题描述 投票:0回答:1

我需要帮助解析从一个基类派生类的具体方法,并将它们添加到委托集合。

我有一大堆的是从单个基类派生类。每个这些类的具有1种或更多的方法与特定的自定义属性[IntentHandler]。这些方法都有一个参数。参数类型是不同,这取决于该方法,但是每种方法的参数是从另一个基类(公共类意图)的。我需要找到所有这些方法,建立一个适当的委托,并将它们添加到委托“字典”,其中关键是参数的类型和值的方法。

现在,我有每个子类创建并注册它的方法委托,但有子类的很多,每个都需要代码来注册它的方法。通过基类使用的System.Reflection我相信我宁愿处理这个问题。这将大大减少代码,并允许更好的扩展版本。

public class StaticService : Service
{
    delegate void ObjectCreatedIntentHandler(ObjectCreatedIntent oci);

    private ObjectCreatedIntentHandler handleObjectCreatedIntent;

    public StaticService()
    {
        handleObjectCreatedIntent = HandleObjectCreatedIntent;
    }

    private void HandleObjectCreatedIntent(ObjectCreatedIntent oci)
    {

    }

    internal override void RegisterIntentHandlers(IntentManager registry)
    {
        registry.RegisterForIntent(typeof(ObjectCreatedIntent), handleObjectCreatedIntent);
    }

    internal override void UnregisterIntentHandlers(IntentManager registry)
    {
        registry.UnregisterForIntent(typeof(ObjectCreatedIntent), handleObjectCreatedIntent);
    }
}

我喜欢的东西线沿线的更多:

public class StaticService : Service
{
    public StaticService()
    {
    }

    [IntentHandler]
    private void HandleObjectCreatedIntent(ObjectCreatedIntent oci)
    {

    }
}

和基类

public class Service
{
    public Service()
    {
        RegisterIntents()
    }

    private void RegisterIntents()
    {
        // Find all classes derived from Service
        // Find each method in those classes with the [IntentHandler]
        // Attribute
        // Get the method's Intent Class derived parameter type
        // create a delegate I can invoke later for that method.
        // Add the delegate to a Dictionary<Intent,Delegate>;
    }
}
c# attributes delegates
1个回答
0
投票

我想我找到了解决办法。我使用的,而不是代表现在采取行动。

在我的基类,我现在这样做:

    internal void RegisterIntentHandlers(IntentManager registry)
    {
        RegisterIntentHandlers(this.GetType(), registry);
    }

    internal void RegisterIntentHandlers(Type klass, IntentManager registry)
    {
        foreach (System.Reflection.MethodInfo m in klass.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
        {
            foreach (Attribute attribute in m.GetCustomAttributes())
            {
                if (attribute is IntentHandler)
                {
                    if (m.GetParameters().Count() == 1)
                    {
                        ParameterInfo p = m.GetParameters()[0];
                        Type paramClass = p.ParameterType;
                        if (paramClass.IsAssignableFrom(typeof(Intent)))
                        {
                            object consumerKey = this.GetType().FullName + "#" + m.Name;
                            Action<Intent> intentConsumer = (Action<Intent>)m.CreateDelegate(typeof(Action<Intent>), this);
                            registry.RegisterForIntent<Intent>(paramClass, consumerKey, intentConsumer);
                            if (!registration.ContainsKey(paramClass))
                                registration[paramClass].Add(consumerKey);
                        }
                    }
                }
            }
        }
    }

我还没有完成测试还没有,但它的权利在我看来。我现在得走了清理所有的子类之前,我可以完成测试。

任何意见/建议吗?

© www.soinside.com 2019 - 2024. All rights reserved.