简体   繁体   中英

how to implement interface through reflection in c#

My Code is like
Assembly assemblyX = Assembly.Load("xyz.abc.DataObjects"); Type t = assemblyX.GetType("xyz.abc.DataObjects." + ValueStr); Everything is good till this point. Now i want to implement interface on 't' which is 'xDataObject' and require a generic ie "CurrentDataObject" in < > operators like xDataObject<CurrentDataObject> . This interface consists many member functions, which I want to use in reflection. According to my research on it, the problem is in this next line, which works fine until we don't need interface ie
Object objClass = (Object)Activator.CreateInstance(t);
Help me. I'm new to Generics. Thank you.

You can take a look at Castle DynamicProxy . It lets you create proxies of your types on run-time which implement interfaces, base classes and many other things.

Check its documentation here .

There is no native solution for what you want as generic checking is made at compile time. At runtime you must use dynamic in your code or the previously recommended dll.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM