调用继承类C#反射的构造函数

调用继承类C#反射的构造函数,c#,reflection,constructor,C#,Reflection,Constructor,我正在使用反射来加载程序集 下面是我将通过反射加载的汇编代码: using BaseDll; namespace CustomLibrary { public class CustomClass : BaseClassofDll { // Creating all constructors as of base class public CustomClass() : base() { } public CustomClass(s

我正在使用反射来加载程序集

下面是我将通过反射加载的汇编代码:

using BaseDll;

namespace CustomLibrary
{
    public class CustomClass : BaseClassofDll
    {
        // Creating all constructors as of base class
        public CustomClass() : base() { }
        public CustomClass(string s1, string s2) : base(s1,s2) { }
        public CustomClass(string s1, string s2, object obj) : base(s1, s2, obj) { }

        public override void Method1() { }
        public override void Method2() { }
    }
}
反射代码:

namespace MyLibrary
{
    public class MyClass
    {
        public void CreateAssemblyClass()
        {
            string dllPath = "C:\Users\xyz\AppData\Local\Temp\CustomLibrary.dll"
            Assembly asm = Assembly.LoadFrom(dllPath);
            // asm is loaded with correct dll

            Type t = asm.GetType("CustomLibrary.CustomClass");
            // t is initialized with correct class

            object[] constructorParameters = new object[2];
            constructorParameters[0] = "123";   // First parameter.
            constructorParameters[1] = "456";   // Second parameter.

            dynamic result = Activator.CreateInstance(t, constructorParameters);
            // Get Exception here : Constructor on type 'CustomLibrary.CustomClass' not found."

            t.GetConstructors();
            // This gives only single constructor of type ctor()
            // My constructor with 2 or 3 parameters is not found. 

            ConstructorInfo ctor = t.GetConstructor(new[] { typeof(string),typeof(string) });
            // This gives null
        }
    }
}
如何使用参数化构造函数创建CustomClass的insatnce。这是必要的,因为我要重写CustomClass中的2个方法,因此我想使用CustomClass而不是BaseClassOfDL

编辑: 由于你们中的许多人都要求共享BaseClassofDll的代码,让我告诉你们BaseDll是一个第三方dll。纵观其定义,它如下所示:

public class BaseClassofDll
{
    public BaseClassofDll();
    public BaseClassofDll(string s1);
    public BaseClassofDll(object obj, string s1);
    public BaseClassofDll(string s1, string s2);
    public BaseClassofDll(object obj, string s1, string s2);

    /*****Other methods ***/
}
使用asm.CreateInstance(“CustomLibrary.CustomClass”); 我想我应该工作。 传递您的参数。我已经硬编码了。

使用asm.CreateInstance(“CustomLibrary.CustomClass”); 我想我应该工作。
传递您的参数。我已经硬编码了。

没有参数的GetConstructors函数将只显示公共的非静态构造函数。请验证您的实际实施是否符合这些条件:

Default flags: BindingFlags.Public | BindingFlags.Instance

不带参数的GetConstructors函数将只显示公共的非静态构造函数。请验证您的实际实施是否符合这些条件:

Default flags: BindingFlags.Public | BindingFlags.Instance

您可以发布基类的代码(以及您拥有的确切可见性修饰符)吗?基类驻留在哪里?第二个选项:你确定你加载了正确的DLL(不是一个旧版本,这个类实际上只有一个构造函数)?看起来不错。您的代码应该可以正常工作。你也可以分享BaseClassofDell吗?@felix-b我已经编辑了我的question@JonSkeet我们要求提供基类的代码,因为我们没有想法了。我打赌也是DLL的旧版本。看不出这种行为的任何其他原因。@Vandita使用反汇编工具(例如,Telerik JustDecompile可在上获得),打开您试图加载的DLL,然后重新检查其中实际驻留的代码。最有可能的是,您会发现那里的CustomClass与您发布的示例不同。您能发布基类的代码(使用您拥有的确切可见性修饰符)吗?基类驻留在哪里?第二个选项:你确定你加载了正确的DLL(不是一个旧版本,这个类实际上只有一个构造函数)?看起来不错。您的代码应该可以正常工作。你也可以分享BaseClassofDell吗?@felix-b我已经编辑了我的question@JonSkeet我们要求提供基类的代码,因为我们没有想法了。我打赌也是DLL的旧版本。看不出这种行为的任何其他原因。@Vandita使用反汇编工具(例如,Telerik JustDecompile可在上获得),打开您试图加载的DLL,然后重新检查其中实际驻留的代码。最有可能的是,您会发现那里的CustomClass与您发布的示例不同。问题中的所有3个构造函数都是公共实例构造函数。对该类型的GetConstructors()的调用完全返回所有3个构造函数。还有其他东西在妨碍你。在你的代码中你写了注释:这只给出了一个类型为ctor()@Chrillie I不是OP:)的构造函数,但问题中的代码对我来说就是这样。还有一个问题(我打赌OP试图加载一个旧版本的DLL,但如果我错了,我想也许我们在这里遇到了一个有趣的问题)@felix-b啊,好:)我同意你的看法,我认为示例代码并不完全是他这方面实际运行的代码。问题中的所有3个构造函数都是公共实例构造函数。对该类型的GetConstructors()的调用完全返回所有3个构造函数。还有其他东西在妨碍你。在你的代码中你写了注释:这只给出了一个类型为ctor()@Chrillie I不是OP:)的构造函数,但问题中的代码对我来说就是这样。还有一些问题(我打赌OP试图加载一个旧版本的DLL,但如果我错了,我想也许我们在这里遇到了一个有趣的问题)@felix-b啊,好的:)我同意你的看法,我认为示例代码并不完全是他这方面实际运行的代码。你的代码对我有用。但是,我在创建实例后分配属性。所以这不是正确的解决方案。我想用我的参数化结构实例化这个类。我们的代码正在为我工作。但是,我在创建实例后分配属性。所以这不是正确的解决方案。我想使用参数化构造函数实例化这个类