0

Is it possible to dynamically make an interface in C#, (maybe by using reflection?).

I want to create this interface based on a dictionary with keys and values. Each key would be a field or a property of the newly made interface. There are potentially hundreds of dictionaries like these and I'm trying to find a way to do this dynamically, doesn't matter if there are hundreds of ICustomInterfaceSomeName.cs files generated.

I checked this link about T4 text templates but I can't figure out how to apply it to my issue. I also checked this link but it doesn't really apply to what I am trying to achieve. I see a lot of questions about how to implement an interface dynamically but not how to create it from scratch with some data.

Can someone point me in the right direction or give me some pointers?

Intended usage:

What I'm trying to do is getting a final object that implements the said interfaces to have intellisense with the dictionaries. This is related to this question. The point is making objects dynamically that can implement these interfaces in order to have intellisense while writing code.

I am guessing this would require to build the unfinished code for the interfaces to be generated and then have intellisense.

If the question is a duplicate or isn't clear, don't hesitate to comment so I can remedy it

Community
  • 1
  • 1
Jose Luis
  • 3,307
  • 3
  • 36
  • 53
  • 4
    Can you give an example of the intended usage? I can't understand why you would be trying to do this. – PMF Jul 15 '15 at 08:09
  • @PMF I edited it with some clarification. – Jose Luis Jul 15 '15 at 08:14
  • What about this answer? http://stackoverflow.com/a/1822338/266978 What you need, in general, is reflection: https://msdn.microsoft.com/en-us/library/system.reflection.emit.aspx – Baltasarq Jul 15 '15 at 08:19
  • @Baltasarq from what I see, I would need the interface beforehand, and that is exactly the problem. Except if I can make an interface using reflection? Maybe I missed something? – Jose Luis Jul 15 '15 at 08:27
  • "Each key would be a field or a property of the newly made interface". What do the values represent? What type are the keys, or does it vary? – David Arno Jul 15 '15 at 08:46
  • Since there are hundreds of these dictionaries, how discoverable are they? Are they public and static, all in one assembly, meaning they could be found by reflection? – David Arno Jul 15 '15 at 08:51
  • @DavidArno All the values are strings and all keys are strings as well. The dictionaries can be easily discoverable, they are the return parameter from another method, a method that is public. So I think they can be found by reflection. – Jose Luis Jul 15 '15 at 09:24
  • @Joze, I haven't dug deep, but I suppose you can create an interface the same way you create a class. The key method seems to be DefineType() (5th line in the example above), which basically accepts a name and a collection of TypeAttributes. One of the TypeAttributes is indeed Interface, so DefineType( "MyIntf", TypeAttribute.Interface ) should work. https://msdn.microsoft.com/en-us/library/system.reflection.typeattributes(v=vs.71).aspx – Baltasarq Jul 15 '15 at 09:30

0 Answers0