Hi guys,

I'm dynamically loading a dll that I've written at runtime but for some reason I'm struggling to get the generics working. I keep getting the following error:

Cannot create an instance of XmlPlugin.XmlPlugin`1[T] because Type.ContainsGenericParameters is true

Heres the code that's in the DLL that I'm trying to load..

[Plugin(PluginType.Storage)]

    public class XmlPlugin<T> : IStoragePlugin<T>

    {



        #region IPlugin Members



        public string Name

        {

            get { return "Xml Plugin"; }

        } 



        public string Version

        {

            get { return "0.0.0.1"; }

        }



        public string Author

        {

            get { return "John Rudd"; }

        }



        #endregion



        public void write(T obj)

        {

            Console.Write(obj);

        }





        public string getSupportedTypes()

        {

            return "Xml|*.xml";

        }





        public string iconKey

        {

            get { return "PluginDemo.Resources.dll.bmp"; }

        }



        public T Read()

        {

            return (T)new Object();

        }

    }

Now here's the code that I'm using to load the dll..

if (pt == PluginType.Storage)

                    {

                        object o = Activator.CreateInstance(PluginClass, 2) ;

                    }

Any help would be greatly appreciated.

Recommended Answers

All 2 Replies

Please read this MSDN page.

Excerpt : true if the Type object is itself a generic type parameter or has type parameters for which specific types have not been supplied; otherwise, false. Here is another article about Reflection & generics.

Thanks for that. I've done more reading and now have the following bridge to across!

Type d1 = typeof(IStoragePlugin<>);
                            Type[] typeArgs = { typeof(string) };
                            Type makeme = d1.MakeGenericType(typeArgs);
                            object o = Activator.CreateInstance(makeme);
                            IStoragePlugin<string> itsMe = o as IStoragePlugin<string>;
                            Console.WriteLine((itsMe == null) ? "Failed" : "Succeeded");

Now I've got IStoragePlugin but since I don't know what implements this I can't say
XmlPlugin ..... because it might not be. Basically all I know is that the class I want is an IStoragePlugin. Whether thats XML, CSV....

But now I'm faced with the following error:

Cannot create an instance of an interface.

Can this not be done?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.