Search code examples
c#genericsapp-configunity-container

Including a generic class in Unity App.Config file


I have a class of type ISimpleCache<IBrokeredDataObject> that I want to add as a type alias (then a type) in the App.Config file

the line

<typeAlias alias="ISimpleCacheOfIBrokeredDataObject" type="MyApplication.ISimpleCache<IBrokeredDataObject>, MyApplication" />

is obviously wrong due to the <>, however I'm not convinced escaping them;

<typeAlias alias="ISimpleCacheOfIBrokeredDataObject" type="MyApplication.ISimpleCache&lt;IBrokeredDataObject&gt;, MyApplication" />

is correct either.

I am currently ripping my code apart to use Unity, so am too far from a compilable code base to test this quickly, and was hoping to get some confirmation here.


Solution

  • Check out this blog post:

    In order to write a generic type, use the ` sign followed by the number of generic types that the interface/class receives.

    And a comment in the same page said:

    In order to use a constant type in the generic you need to use brackets ([[ ]]).

    So I guess your configuration file should contain something like this:

    <typeAlias alias="ISimpleCacheOfIBrokeredDataObject"
       type="MyApplication.ISimpleCache`1[[MyApplication.IBrokeredDataObject, MyApplication]], MyApplication" />
    

    Note the use of the "grave accent" or "backquote" character (`), not the normal single quote (').