0

dll をロードしてインスタンスを作成し、メソッドを呼び出して戻り値を確認したいと考えています。インスタンスの作成時に、例外 {"Parameter count mismatch."} が発生します。

    static void Main(string[] args)
    {

            ModuleConfiguration moduleConfiguration = new ModuleConfiguration();

            // get the module information
            if (!moduleConfiguration.getModuleInfo())
                throw new Exception("Error: Module information cannot be retrieved");


            // Load the dll
            string moduledll =  Directory.GetCurrentDirectory() + "\\" +
                                                       moduleConfiguration.moduleDLL;
            testDLL = Assembly.LoadFile(moduledll);

            // create the object
            string fullTypeName = "MyNameSpace."+ moduleConfiguration.moduleClassName;
            Type moduleType = testDLL.GetType(fullTypeName);

            Type[] types = new Type[1];
            types[0] = typeof(string[]);

            ConstructorInfo constructorInfoObj = moduleType.GetConstructor(
                        BindingFlags.Instance | BindingFlags.Public, null,
                        CallingConventions.HasThis, types, null);

            if (constructorInfoObj != null)
            {
                Console.WriteLine(constructorInfoObj.ToString());
                constructorInfoObj.Invoke(args);
            }

The constructor for the class in dll is:
public class SampleModule:ModuleBase
{
    /// <summary>
    /// Initializes a new instance of the <see cref="SampleModule" /> class.
    /// </summary> 
    public SampleModule(string[] args)
        : base(args)
    {
        Console.WriteLine("Creating SampleModule"); 
    }

Q: 1. 何が間違っていますか? 2. メソッドを取得して呼び出し、戻り値を取得するにはどうすればよいですか? 3.これを行うためのより良い方法はありますか?

4

1 に答える 1

1

次の行を追加するだけで済みます。

Object[] param = new Object[1] { args };

前:

constructorInfoObj.Invoke(args);

ConstructorInfo を使用しない代替 (短い) ソリューション:

        :

       // create the object
        string fullTypeName = "MyNameSpace."+ moduleConfiguration.moduleClassName;
        Type moduleType = testDLL.GetType(fullTypeName);

        Object[] param = new Object[1] { args };
        Activator.CreateInstance(runnerType, param);
于 2013-03-26T22:36:45.857 に答える