<Type> 元素 (.NET Native)
将运行时策略应用到一个特定类型,例如一个类或结构。
语法
<Type Name="type_name"
Activate="policy_type"
Browse="policy_type"
Dynamic="policy_type"
Serialize="policy_type"
DataContractSerializer="policy_setting"
DataContractJsonSerializer="policy_setting"
XmlSerializer="policy_setting"
MarshalObject="policy_setting"
MarshalDelegate="policy_setting"
MarshalStructure="policy_setting" />
特性和元素
下列各节描述了特性、子元素和父元素。
特性
属性 | 属性类型 | 说明 |
---|---|---|
Name |
常规 | 必需的特性。 指定类型名称。 |
Activate |
反射 | 可选特性。 控制运行时对构造函数的访问,以启用实例激活。 |
Browse |
反射 | 可选特性。 控制对有关程序元素信息的查询,但并不启用任何运行时访问。 |
Dynamic |
反射 | 可选特性。 控制运行时对所有类型成员的访问,包括构造函数、方法、字段、属性和事件,以启用动态编程。 |
Serialize |
序列化 | 可选特性。 控制运行时对构造函数、字段和属性的访问,使类型实例得到序列化和反序列化处理,这是通过库进行的,例如 Newtonsoft JSON 序列化程序。 |
DataContractSerializer |
序列化 | 可选特性。 控制使用 System.Runtime.Serialization.DataContractSerializer 类的序列化策略。 |
DataContractJsonSerializer |
序列化 | 可选特性。 控制使用 System.Runtime.Serialization.Json.DataContractJsonSerializer 类的 JSON 序列化策略。 |
XmlSerializer |
序列化 | 可选特性。 控制使用 System.Xml.Serialization.XmlSerializer 类的 XML 序列化策略。 |
MarshalObject |
Interop | 可选特性。 控制封送引用类型到 Windows 运行时和 COM 的策略。 |
MarshalDelegate |
Interop | 可选特性。 控制将委托类型作为函数指针封送到本机代码的策略。 |
MarshalStructure |
Interop | 可选特性。 控制封送处理值类型到本机代码的策略。 |
Name 特性
Value | 说明 |
---|---|
type_name | 类型名称。 如果此 <Type> 元素是 <命名空间> 元素或另一个 <Type> 元素的子元素,type_name 可能会包括类型名称,而不包括其命名空间。 否则,type_name 必须包含完全限定的类型名称。 |
所有其他特性
Value | 说明 |
---|---|
策略_设置 | 该设置将应用到这种策略类型。 可能值为 All 、Auto 、Excluded 、Public 、PublicAndInternal 、Required Public 、Required PublicAndInternal 以及 Required All 。 有关详细信息,请参阅运行时指令策略设置。 |
子元素
元素 | 说明 |
---|---|
<AttributeImplies> | 如果包含类型是一个特性,为该特性所应用到的代码元素定义一个运行时策略。 |
<Event> | 将反射策略应用到属于这种类型的一个事件。 |
<字段> | 将反射策略应用到属于这种类型的一个字段。 |
<泛型参数> | 将策略应用到一个泛型类型的参数类型。 |
<ImpliesType> | 如果该策略已应用到以包含 <Type> 元素为代表的类型,将该策略应用到一个类型。 |
<方法> | 将反射策略应用到属于这种类型的一个方法。 |
<MethodInstantiation> | 将反射策略应用到属于这种类型的一个构造泛型方法。 |
<属性> | 将反射策略应用到属于这种类型的一个属性。 |
<Subtypes> | 将运行时策略应用到从包含类型继承的所有类。 |
<Type> |
将反射策略应用到一个嵌套类型。 |
<TypeInstantiation> | 将反射策略应用到一个构造泛型类型。 |
父元素
元素 | 说明 |
---|---|
<应用程序> | 作为应用程序范围内的类型和元数据可以反应在运行时间的类型成员的容器而服务。 |
<Assembly> | 将反射策略应用到指定程序集中的所有类型。 |
<库> | 定义包含元数据在运行时间可以用于反射的类型和类型成员的程序集。 |
<命名空间> | 将反射策略应用到命名空间中的所有类型。 |
<Type> |
将反射策略应用到一种类型及其所有成员。 |
<TypeInstantiation> | 将反射策略应用到一种构造泛型类型及其所有成员。 |
注解
反射、序列化和互操作特性都是可选项。 如果这些都不存在,<Type>
元素会充当容器,其子类型为独立成员定义策略。
如果一个 <Type>
元素是 <Assembly>、<Namespace>、<Type>
或 <TypeInstantiation> 元素的子元素,它会替代由父元素定义的策略设置。
一个泛型类型的 <Type>
元素会将其策略应用到所有不具有自身策略的实例化。 构造泛型类型的策略是由 <TypeInstantiation> 元素定义的。
如果该类型是一个泛型类型,其名称包含一个重读音符 (`),后面还跟着其泛型参数的编号。 例如,Name
的 <Type>
元素 System.Collections.Generic.List<T> 特性为 Name="System.Collections.Generic.List`1"
。
示例 1
以下实例使用反射来展示 System.Collections.Generic.List<T> 类的字段、属性和方法。 示例中的 b
变量是 TextBlock 控件。 因为实例仅仅检索了类型信息,元数据的可用性是由 Browse
策略设置控制的。
public static void GetReflectionInfo()
{
Type t = typeof(List<>);
b.Text += String.Format("Type information for {0}\n", t);
// Get fields.
b.Text += "\nFields:\n";
var fields = t.GetTypeInfo().DeclaredFields;
int nFields = 0;
foreach (var field in fields)
{
b.Text += String.Format(" {0} ({1})", field.Name, field.FieldType.Name);
nFields++;
}
if (nFields == 0) b.Text += " None\n";
// Get properties.
b.Text += "\nProperties:\n";
var props = t.GetTypeInfo().DeclaredProperties;
int nProps = 0;
foreach (var prop in props)
{
b.Text += String.Format(" {0} ({1})\n", prop.Name, prop.PropertyType.Name);
nProps++;
}
if (nProps == 0) b.Text += " None\n";
// Get methods.
b.Text += "\nMethods:\n";
var methods = t.GetTypeInfo().DeclaredMethods;
int nMethods = 0;
foreach (var method in methods)
{
if (method.IsSpecialName) continue;
b.Text += String.Format(" {0}({1}) ({2})\n", method.Name,
GetSignature(method), method.ReturnType.Name);
nMethods++;
}
if (nMethods == 0) b.Text += " None\n";
}
private static string GetSignature(MethodInfo m)
{
string signature = null;
var parameters = m.GetParameters();
for (int ctr = 0; ctr < parameters.Length; ctr++)
{
signature += String.Format("{0} {1}", parameters[ctr].ParameterType.Name,
parameters[ctr].Name);
if (ctr < parameters.Length - 1) signature += ", ";
}
return signature;
}
由于 List<T> 类的元数据不会由 .NET Native 工具链自动包含,因此该示例在运行时无法显示请求的成员信息。 为提供所需的元数据,将以下 <Type>
元素添加到运行时指令文件。 请注意,因为我们已经提供了父 <Namespace> 元素,因此不必在 <Type>
元素中提供完全限定的类型名称。
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Application>
<Assembly Name="*Application*" Dynamic="Required All" />
<Namespace Name ="System.Collections.Generic" >
<Type Name="List`1" Browse="Required All" />
</Namespace>
</Application>
</Directives>
示例 2
以下实例使用了反射来检索一个代表 PropertyInfo 属性的 String.Chars[] 对象。 它使用 PropertyInfo.GetValue(Object, Object[]) 方法来检索一个字符串中的七个字符的值,并在该字符串中显示所有字符。 示例中的 b
变量是 TextBlock 控件。
public void Example()
{
string test = "abcdefghijklmnopqrstuvwxyz";
// Get a PropertyInfo object.
TypeInfo ti = typeof(string).GetTypeInfo();
PropertyInfo pinfo = ti.GetDeclaredProperty("Chars");
// Show the seventh letter ('g')
object[] indexArgs = { 6 };
object value = pinfo.GetValue(test, indexArgs);
b.Text += String.Format("Character at position {0}: {1}\n", indexArgs[0], value);
// Show the complete string.
b.Text += "\nThe complete string:\n";
for (int x = 0; x < test.Length; x++)
{
b.Text += pinfo.GetValue(test, new Object[] {x}).ToString() + " ";
}
}
// The example displays the following output:
// Character at position 6: g
//
// The complete string:
// a b c d e f g h i j k l m n o p q r s t u v w x y z
由于 String 对象的元数据不可用,因此在使用 .NET Native 工具链编译时,对 PropertyInfo.GetValue(Object, Object[]) 方法的调用会在运行时引发 NullReferenceException 异常。 要消除异常并提供必需的元数据,请将以下 <Type>
元素添加到运行时指令文件:
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Application>
<Assembly Name="*Application*" Dynamic="Required All" />
<Type Name="System.String" Dynamic="Required Public"/> -->
</Application>
</Directives>