I have a library, and I use an Interface to define the required members of the clients “User” object in order for the client’s type to work with my library. As long as they implement the members in my interface, everything is fine. My Inferface implements IPrincipal, so it’s a standard .Net Framework user type. It can be used in ASP.NET seamlessly. When coding my library, I don’t know or have access to the client’s type that will implement my interface. How, then, can I get it at runtime and code against it? I have static and instance calls to methods in my interface. The answer: Reflection and runtime-loading of the client’s type. Here’s some code I’m using to do it:
public static Type ClientType
ClientAssembly = Assembly.Load(ConfigurationSettings.AppSettings[“UserAssemblyName”]);
Type clientType = ClientAssembly.GetType(ConfigurationSettings.AppSettings[“UserTypeName”], true, false);
This is a static property in my library to encapsulate the getting of the client’s type. The client will have to define in the web.config file the assembly and namespace, and then we’re good. Once I have the type, I can invoke the members using methods of the Type object. I know the members I need to invoke because I’ve defined an interface, so the client’s object MUST have the members I need. Now the client can have their own user object, and once they implement my interface, they are completely interoperable with my library which will make heavy use of their custom type.