0

I am loading a DLL up at runtime. The DLL and the main application both use a DLL witch holds interfaces that let the program know how to use some of the Types in the DLL. In the main application there is a Factory class where the Dll can set one of its object types to be created when the main application requests the interface it inherits from. below is the striped down (mainly removed error handling code) version of the function to create the object type from the DLL. When this gets called I get a Exception saying no parameterless constructor defined for this object. I dont know why because they all have parameterless constructors.

    //inside the DLL
    Factory.ResovleType<ISomething>(typeof(SomethingCool));

    //inside the main application
    ISomething obj = Factory.CreateObject<ISomething>();


    //inside the Factory Class
    public static T CreateObject<T>(params object[] args) 
    {
        T obj = (T)Activator.CreateInstance(resovledList[typeof(T)], args);

        return obj;
    }

    public static void ResolveType<T>(Type type)
    {
          resovledList.Add(typeof(T), type);
4

1 に答える 1

2

コメントから:

によって検出されたresovledList[typeof(T)]型は、意図した型ではありません。代わりに、それはRuntimeTypeです。ResolveType<ISomething>(typeof(Something).GetType())これは、次の代わりに呼び出した結果である可能性がありResolveType<ISomething>(typeof(Something))ます。

typeof(Something)Typeは(実際RuntimeTypeには から派生した)型の値なTypeので、 を呼び出すとtypeof(Something).GetType()が得られますtypeof(RuntimeType)

結局のところ、あなたは実際には別の場所に電話をかけGetType()ていましたが、問題と解決策は同じですGetType()。ここで電話するべきではありません。

于 2013-09-21T20:35:29.430 に答える