FieldBuilder.ReflectedType Property

Definition

Indicates the reference to the Type object from which this object was obtained. This property is read-only.

C#
public override Type? ReflectedType { get; }
C#
public override Type ReflectedType { get; }

Property Value

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

Remarks

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.

C#
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Permissions;

public class FieldBuilder_Sample
{
   private static Type CreateType(AppDomain currentDomain)
   {

      // Create an assembly.
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "DynamicAssembly";
      AssemblyBuilder myAssembly =
         currentDomain.DefineDynamicAssembly(myAssemblyName,AssemblyBuilderAccess.Run);
      // 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",TypeAttributes.Public);

      // 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 = myTypeBuilder.DefineConstructor(
         MethodAttributes.Public, CallingConventions.Standard, constructorArgs);
      ILGenerator constructorIL = myConstructor.GetILGenerator();
      constructorIL.Emit(OpCodes.Ldarg_0);
      ConstructorInfo superConstructor = typeof(Object).GetConstructor(new Type[0]);
      constructorIL.Emit(OpCodes.Call, superConstructor);
      constructorIL.Emit(OpCodes.Ldarg_0);
      constructorIL.Emit(OpCodes.Ldarg_1);
      constructorIL.Emit(OpCodes.Stfld, myFieldBuilder);
      constructorIL.Emit(OpCodes.Ret);

      // Create the MyMethod method.
      MethodBuilder myMethodBuilder= myTypeBuilder.DefineMethod("MyMethod",
         MethodAttributes.Public,typeof(String),null);
      ILGenerator methodIL = myMethodBuilder.GetILGenerator();
      methodIL.Emit(OpCodes.Ldarg_0);
      methodIL.Emit(OpCodes.Ldfld, myFieldBuilder);
      methodIL.Emit(OpCodes.Ret);
      if (myFieldBuilder.Attributes.Equals(FieldAttributes.Static))
      {
            Console.WriteLine("Field attribute defined as Static");
      }
      else if(myFieldBuilder.Attributes.Equals(FieldAttributes.Static|FieldAttributes.Private))
      {
         Console.WriteLine("Field attributes are Static and Private");
      }
      Console.WriteLine("ReflectedType of Field is: " + myFieldBuilder.ReflectedType);

      return myTypeBuilder.CreateType();
   }

   public static void Main()
   {
      Type myType = CreateType(Thread.GetDomain());
      // Create an instance of the "HelloWorld" class.
      Object helloWorld = Activator.CreateInstance(myType, new Object[] { "HelloWorld" });
      // Invoke the "MyMethod"  of the "MyClass".
      Object myObject  = myType.InvokeMember("MyMethod",
         BindingFlags.InvokeMethod, null, helloWorld, null);
      Console.WriteLine("MyClass.MyMethod returned: \"" + myObject + "\"");
   }
}

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1