اللغة
تحرير

Reflection in .NET Framework for Windows Store Apps

.NET Framework includes a set of reflection types and members for use in Windows 8.x Store apps. These types and members are available in the full .NET Framework and in .NET for Windows Store apps. This article explains the major differences between these and their counterparts in .NET Framework 4 and earlier versions.

If you are creating a Windows 8.x Store app, you must use the reflection types and members in .NET for Windows 8.x Store apps. These types and members are also available, but not required, for use in desktop apps, so you can use the same code for both types of apps.

TypeInfo and Assembly Loading

In .NET for Windows 8.x Store apps, the TypeInfo class contains some of the functionality of the .NET Framework 4 Type class. A Type object represents a reference to a type definition, whereas a TypeInfo object represents the type definition itself. This enables you to manipulate Type objects without necessarily requiring the runtime to load the assembly they reference. Getting the associated TypeInfo object forces the assembly to load.

TypeInfo contains many of the members available on Type, and many of the reflection properties in .NET for Windows 8.x Store apps return collections of TypeInfo objects. To get a TypeInfo object from a Type object, use the GetTypeInfo method.

Query Methods

In .NET for Windows 8.x Store apps, you use the reflection properties that return IEnumerable<T> collections instead of methods that return arrays. Reflection contexts can implement lazy traversal of these collections for large assemblies or types.

The reflection properties return only the declared methods on a particular object instead of traversing the inheritance tree. Moreover, they do not use BindingFlags parameters for filtering. Instead, filtering takes place in user code, by using LINQ queries on the returned collections. For reflection objects that originate with the runtime (for example, as the result of typeof(Object)), traversing the inheritance tree is best accomplished by using the helper methods of the RuntimeReflectionExtensions class. Consumers of objects from customized reflection contexts cannot use these methods, and must traverse the inheritance tree themselves.

Restrictions

In a Windows 8.x Store app, access to some .NET Framework types and members is restricted. For example, you can't call .NET Framework methods that aren't included in .NET for Windows 8.x Store apps, by using a MethodInfo object. In addition, certain types and members that aren't considered safe within the context of a Windows 8.x Store app are blocked, as are Marshal and WindowsRuntimeMarshal members. This restriction affects only .NET Framework types and members; you can call your code or third-party code as you normally would.

See also