共用方式為


FieldBuilder.ReflectedType Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Overrides ReadOnly Property ReflectedType As Type
public override Type ReflectedType { get; }

Property Value

Type: System.Type
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.

Examples

The following code sample illustrates the use of ReflectedType.

Imports System.Reflection
Imports System.Reflection.Emit

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      ' Create an assembly.
      Dim myAssemblyName As New AssemblyName("DynamicAssembly")
      Dim myAssembly As AssemblyBuilder = _
         AppDomain.CurrentDomain.DefineDynamicAssembly(myAssemblyName, _
                                             AssemblyBuilderAccess.Run)
      ' Create a dynamic module in Dynamic Assembly.
      Dim myModuleBuilder As ModuleBuilder = myAssembly.DefineDynamicModule("MyModule")
      ' Define a public class named "MyClass" in the assembly.
      Dim myTypeBuilder As TypeBuilder = myModuleBuilder.DefineType("MyClass", _
                                          TypeAttributes.Public)

      ' Define a private String field named "MyField" in the type.
      Dim myFieldBuilder As FieldBuilder = myTypeBuilder.DefineField("MyField", _
                              GetType(String), FieldAttributes.Private Or FieldAttributes.Static)

      ' Create the constructor.
      Dim constructorArgs As Type() ={GetType(String)}
      Dim myConstructor As ConstructorBuilder = _
                     myTypeBuilder.DefineConstructor(MethodAttributes.Public, _
                              CallingConventions.Standard, constructorArgs)
      Dim constructorIL As ILGenerator = myConstructor.GetILGenerator()
      constructorIL.Emit(OpCodes.Ldarg_0)
      Dim superConstructor As ConstructorInfo = GetType(Object).GetConstructor(New Type() { })
      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.
      Dim myMethodBuilder As MethodBuilder = myTypeBuilder.DefineMethod("MyMethod", _
                           MethodAttributes.Public, GetType(String), Nothing)
      Dim methodIL As ILGenerator = myMethodBuilder.GetILGenerator()
      methodIL.Emit(OpCodes.Ldarg_0)
      methodIL.Emit(OpCodes.Ldfld, myFieldBuilder)
      methodIL.Emit(OpCodes.Ret)

      If 0 <> (myFieldBuilder.Attributes And FieldAttributes.Static) Then    
         outputBlock.Text &= "MyField defined as Static" & vbLf
      End If

      Dim access As FieldAttributes = _
         myFieldBuilder.Attributes And FieldAttributes.FieldAccessMask
      If access = FieldAttributes.Private Then
         outputBlock.Text &= "MyField is private" & vbLf
      End If

      outputBlock.Text &= "ReflectedType of FieldBuilder is: " & _
         myFieldBuilder.ReflectedType.ToString() & vbLf

      Dim myType As Type = myTypeBuilder.CreateType()

      Dim helloWorld As Object = Activator.CreateInstance(myType, New Object() { "HelloWorld" })

      ' Invoke the "MyMethod"  of the "MyClass".
      Dim myObject As Object = myType.InvokeMember("MyMethod", _
               BindingFlags.InvokeMethod, Nothing, helloWorld, Nothing)
      outputBlock.Text &= "MyClass.MyMethod returned: """ & myObject.ToString() & """" & vbLf
   End Sub 
End Class 

' This example produces the following output:
'
'MyField defined as Static 
'MyField is private
'ReflectedType of FieldBuilder is: MyClass
'MyClass.MyMethod returned: "HelloWorld"
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 = 
         AppDomain.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[]{});
      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 (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"
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

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