FieldBuilder.ReflectedType Property

Gets the reference to the Type object from which this object was obtained.

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

public override Type ReflectedType { get; }

Property Value

Type: System.Type
A reference to the Type object from which this instance was obtained.

A FieldBuilder object represents a field of a particular class. In order to obtain a FieldBuilder object, the Type object that represents the class that supports the field is queried. This property holds a reference to that Type object.

The following code sample illustrates the use of ReflectedType.

using System.Reflection;
using System;
using System.Reflection.Emit;

public class Example
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      // Create an assembly.
      AssemblyName myAssemblyName = new AssemblyName("DynamicAssembly");
      AssemblyBuilder myAssembly = 
      // Create a dynamic module in Dynamic Assembly.
      ModuleBuilder myModuleBuilder = myAssembly.DefineDynamicModule("MyModule");
      // Define a public class named "MyClass" in the assembly.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyClass", 

      // Define a private String field named "MyField" in the type.
      FieldBuilder myFieldBuilder = myTypeBuilder.DefineField("MyField", 
          typeof(string), FieldAttributes.Private | FieldAttributes.Static);

      // Create the constructor.
      Type[] constructorArgs = {typeof(string)};
      ConstructorBuilder myConstructor = 
            CallingConventions.Standard, constructorArgs);
      ILGenerator constructorIL = myConstructor.GetILGenerator();
      ConstructorInfo superConstructor = typeof(object).GetConstructor(new Type[]{});
      constructorIL.Emit(OpCodes.Call, superConstructor);
      constructorIL.Emit(OpCodes.Stfld, myFieldBuilder);

      // Create the MyMethod method.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("MyMethod", 
                                        MethodAttributes.Public, typeof(string), null);
      ILGenerator methodIL = myMethodBuilder.GetILGenerator();
      methodIL.Emit(OpCodes.Ldfld, myFieldBuilder);

      if (0 != (myFieldBuilder.Attributes & FieldAttributes.Static))
         outputBlock.Text += "MyField defined as Static\n";

      FieldAttributes access = 
         myFieldBuilder.Attributes & FieldAttributes.FieldAccessMask;
      if (access == FieldAttributes.Private)
         outputBlock.Text += "MyField is private\n";

      outputBlock.Text += "ReflectedType of FieldBuilder is: " + 
         myFieldBuilder.ReflectedType.ToString() + "\n";

      Type myType = myTypeBuilder.CreateType();

      object helloWorld = Activator.CreateInstance(myType, new object[]{"HelloWorld"});

      // Invoke the "MyMethod"  of the "MyClass".
      object myObject = myType.InvokeMember("MyMethod", BindingFlags.InvokeMethod, 
         null, helloWorld, null);
      outputBlock.Text += "MyClass.MyMethod returned: \"" + myObject.ToString() + "\"\n";

/* This example produces the following output:

MyField defined as Static 
MyField is private
ReflectedType of FieldBuilder is: MyClass
MyClass.MyMethod returned: "HelloWorld"


Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions