-1

そのため、アプリケーションのメソッドを dll から外部で呼び出す方法を探しています。(以下の例を参照)これは私が試みていることですが、a)機能していない、b)機能していた場合、DynamicInvokeの呼び出しが非常に遅くなると感じています。

まず第一に、この方法でやりたい場合は、現在、callthisexternally() の戻り値の型が間違っているというエラーが発生するため、戻り値の型をどのように処理すればよいですか。

これを行うより良い方法はありますか?

--- within a a dll ---
public class mydll
{
    // etc.. blah blah
    public object callfromdll(string commandName, int requiredArgs, Delegate method)
    {
        // do stuff
        // now invoke the method
        return method.DynamicInvoke(method.Method.GetParameters().Select(p => p.ParameterType).ToArray());
    }
}
-- within an application that's refrancing the above dll --
public someclass
{
    // etc.. stuff here
    mydll m = new mydll();
    m.callfromdll("callthisexternally", 0, new Action(callthisexternally));
    // the function to be called externally
    public string callthisexternally()
    {
        // do stuff
        return "i was called!";
    }
}
4

3 に答える 3

0

callFromDll が行うべきことの詳細がなくても、Func Delegateを使用して簡単にこれを行うことができます

public class mydll
{
    // etc.. blah blah
    public T callfromdll<T>(string commandName, int requiredArgs, Func<T> method)
    {
        // do stuff
        // now invoke the method
        return method();
    }
}

do stuffを生成するために何かをしていた場合intは、正しいメソッド署名を使用する必要があります。

public class mydll
{
    // etc.. blah blah
    public T callfromdll<T>(string commandName, int requiredArgs, Func<int, T> method)
    {
        int x = SomeComplexFunction(commandName, requiredArgs);
        return method(x);
    }
}
-- within an application that's refrancing the above dll --
public someclass
{
    public void test()
    {
        // etc.. stuff here
        mydll m = new mydll();
        var result = m.callfromdll("callthisexternally", 0, new Func(callthisexternally));
        //result contains "i was called, and my result was #" and where # is replace with the number passed in to callthisexternally
    }

    // the function to be called externally
    public string callthisexternally(int x)
    {
        // do stuff
        return "i was called, and my result was " + x;
    }
}

これで、DLL は x に対して計算した値を、渡された関数に渡し、その関数からの結果を返します。

于 2013-07-16T22:14:30.083 に答える