C# 在 Unity App.Config 文件中包含通用类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/454726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:00:52  来源:igfitidea点击:

Including a generic class in Unity App.Config file

c#genericsapp-configunity-container

提问by johnc

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

我有一个类型ISimpleCache<IBrokeredDataObject>,我想在 App.Config 文件中添加为类型别名(然后是类型)

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.

我目前正在分解我的代码以使用 Unity,所以离可编译的代码库太远,无法快速测试,并希望在这里得到一些确认。

采纳答案by Hosam Aly

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

请注意使用“重音符”或“反引号”字符 ( `),而不是普通的单引号 ( ')。

回答by Pavel Savara

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" />

回答by Alex Vilela

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**[** ).

如果您愿意,您可以在不同的行上使用每种类型,以便更容易理解。请注意,第一个括号必须紧跟在类型之后( FamilySection`2**[** )。

回答by ken

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

此处记录了 Type.GetType 方法(字符串)的语法: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]]")