微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Activator.CreateInstance 与 Delegate 参数

如何解决Activator.CreateInstance 与 Delegate 参数

我想使用反射从第三方程序集创建内部类的实例。

这个类看起来像这样

    internal sealed class SomeClass
    {
        public delegate object SomeDelegate(object value);

        public SomeDelegateHandler { get; private set; }

        public SomeClass(SomeDelegate handler)
        {
            this.Handler = handler;
        }
    }

通常我会使用反射来创建内部类的实例,但我需要传递一个 SomeDelegate 委托。

由于该委托位于内部类中,因此我也需要通过反射创建该委托的实例。但是我已经尝试过的所有方法都不起作用

这是我目前尝试过的。

// This is the method that I need to pass as an argument
public static object SomeDelegateImplementation(object value)
{
    return value;
}

public void Main()
{
    // example: create SomeClass without reflection 
    // (this is how SomeClass is usually constructed);
    var instance = new SomeClass(SomeDelegateImplementation);

    // setup: get the ConstructorInfo so I can use 
    // ctor.Invoke(...) to create an instance of that class
    var assembly = typeof(ThirdParty.OtherClass).Assembly;
    var type = assembly.GetType("ThirdParty.someClass",true,true);
    var ctor = type.GetConstructors()[0];

    // method one (doesn't work)
    // compiler error: cannot convert method group 'SomeDelegateImplementation' to non-delegate type 'object'. Did you intend to invoke the method?
    var args = new object[]{ SomeDelegateImplementation }; 
    var instance = ctor.Invoke(args);

    // method two (doen't work)
    // throws a runtime error during invoke: 
    // error converting object with type "System.Func`2[System.Object,System.Object]" to type "ThirdParty.someClass+SomeDelegate".
    Func<object,object> someDelegateImplementation = SomeDelegateImplementation;
    var args = new object[]{ (Delegate)someDelegateImplementation }; 
    var instance = ctor.Invoke(args);
}

解决方法

解决方案

感谢@JonSkeet,我设法使用 SomeClass 来创建 Delegate.CreateDelegate 的实例

    Assembly assembly = typeof(ThirdParty.OtherClass).Assembly;
    Type type = assembly.GetType("ThirdParty.SomeClass",true,true);
    ConstructorInfo ctor = type.GetConstructors()[0];

    // get a reference to the original delegate type
    Type someDelegateHandler Type =
        assembly.GetType("ThirdParty.SomeClass+SomeDelegateHandler",true);

    // get a reference to my method
    MethodInfo someDelegateImplementationMethod = 
        typeof(Program).GetMethod("SomeDelegateImplementation",BindingFlags.Static | BindingFlags.NonPublic);

    // create a delegate that points to my method
    Delegate someDelegateImplementationDelegate = 
        Delegate.CreateDelegate(
            someDelegateHandler,someDelegateImplementationMethod);

    object[] args = new object[]{ someDelegateImplementationDelegate  };
    object instance = ctor.Invoke(args);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。