Training
Module
Get started with classes and objects in C# - Training
Learn how to create classes and instantiate objects that expose encapsulated field data by using class definitions, constructors, and the 'new' operator.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The classes in the System.Reflection namespace, together with System.Type, enable you to obtain information about loaded assemblies and the types defined within them, such as classes, interfaces, and value types (that is, structures and enumerations). You can also use reflection to create type instances at run time, and to invoke and access them.
Assemblies contain modules, modules contain types, and types contain members. Reflection provides objects that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can then invoke the type's methods or access its fields and properties. Typical uses of reflection include the following:
public
or private
), and implementation details (such as abstract
or virtual
) of a constructor. Use the GetConstructors or GetConstructor method of a Type to invoke a specific constructor.abstract
or virtual
) of a method. Use the GetMethods or GetMethod method of a Type to invoke a specific method.static
) of a field, and to get or set field values.The classes of the System.Reflection.Emit namespace provide a specialized form of reflection that enables you to build types at run time.
Reflection can also be used to create type browsers, which enable users to select types and then view the information about those types.
There are other uses for reflection. Compilers for languages such as JScript use reflection to construct symbol tables. The classes in the System.Runtime.Serialization namespace use reflection to access data and to determine which fields to persist. The classes in the System.Runtime.Remoting namespace use reflection indirectly through serialization.
Reflection provides classes, such as Type and MethodInfo, to represent types, members, parameters, and other code entities. However, when you use reflection, you don't work directly with these classes, most of which are abstract (MustInherit
in Visual Basic). Instead, you work with types provided by the common language runtime (CLR).
For example, when you use the C# typeof
operator (GetType
in Visual Basic) to obtain a Type object, the object is really a RuntimeType
. RuntimeType
derives from Type and provides implementations of all the abstract methods.
These runtime classes are internal
(Friend
in Visual Basic). They are not documented separately from their base classes, because their behavior is described by the base class documentation.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Training
Module
Get started with classes and objects in C# - Training
Learn how to create classes and instantiate objects that expose encapsulated field data by using class definitions, constructors, and the 'new' operator.