Question

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.

Was it helpful?

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 (').

OTHER TIPS

I would have rather commented on the answer above, but my score isn't high enough.

The syntax is documented for the Type.GetType Method (string) here: http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx

There are a bunch of examples, a few of which I pasted below.

A generic type with one type argument

Type.GetType("MyGenericType`1[MyType]")

A generic type with two type arguments

Type.GetType("MyGenericType`2[MyType,AnotherType]")

A generic type with two assembly-qualified type arguments

Type.GetType("MyGenericType`2[[MyType,MyAssembly],[AnotherType,AnotherAssembly]]")

An assembly-qualified generic type with an assembly-qualified type argument

Type.GetType("MyGenericType`1[[MyType,MyAssembly]],MyGenericTypeAssembly")

A generic type whose type argument is a generic type with two type arguments

Type.GetType("MyGenericType`1[AnotherGenericType`2[MyType,AnotherType]]")

And this is how you use a type that receives two generic types:

<section name="doubleFamilyConfig"
         type="ConfigTest.Configuration.FamilySection`2[
               [ConfigTest.Types.Child, ConfigTest],
               [ConfigTest.Types.Parent, ConfigTest]
               ],
               ConfigTest" />

You can use each type on a different line if you wish, so that it is easier to understand. Note that the first bracket must be right after the type ( FamilySection`2**[** ).

And this is strongly signed type as generic parameter.

<typeAlias alias="IPublisherOfXElement" type="MyLib.IX.IPublisher`1[[System.Xml.Linq.XElement, System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], MyLib.IX" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top