Поделиться через


ModuleBuilder.DefineType Method (String, TypeAttributes)

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

Defines a type with the specified type name and attributes in this module.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Function DefineType ( _
    name As String, _
    attr As TypeAttributes _
) As TypeBuilder
[SecuritySafeCriticalAttribute]
public TypeBuilder DefineType(
    string name,
    TypeAttributes attr
)

Parameters

  • name
    Type: System.String
    The full name of the type, including the namespace. name cannot contain embedded nulls.

Return Value

Type: System.Reflection.Emit.TypeBuilder
A type with the specified name and attributes.

Exceptions

Exception Condition
ArgumentException

A type with the given name exists in the parent assembly of this module.

-or-

Nested type attributes are set on a type that is not nested.

ArgumentNullException

name is null.

Remarks

Type names must be unique within an assembly.

Examples

The following example creates a TypeBuilder in the current dynamic module using CreateType, builds and completes the type, and creates an instance of the type.

Imports System
Imports System.Reflection
Imports System.Reflection.Emit

Public Class Example

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

        ' Create a dynamic assembly. In Silverlight, dynamic assemblies have only
        ' one module, and are always created with AssemblyBuilderAccess.Run.
        '
        Dim asmname As AssemblyName = new AssemblyName("temp")
        Dim asmbuild As AssemblyBuilder = _
            AppDomain.CurrentDomain.DefineDynamicAssembly(asmname, _
                                                          AssemblyBuilderAccess.Run)
        Dim modbuild As ModuleBuilder = asmbuild.DefineDynamicModule("temp")

        ' Define a type. If TypeAttributes.Public is not specified, the default
        ' access is private, and instances of the type cannot be created.
        '
        Dim typebuild1 As TypeBuilder = _
            modbuild.DefineType("TestType", TypeAttributes.Public)

        ' Create the type, and create an instance of the type.
        '
        Dim t As Type = typebuild1.CreateType()
        Dim obj As Object = Activator.CreateInstance(t)

        outputBlock.Text &= t.AssemblyQualifiedName & vbCrLf
        outputBlock.Text &= obj.ToString()
   End Sub
End Class

' This example produces the following output:
'
'TestType, temp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
'TestType
using System;
using System.Reflection;
using System.Reflection.Emit;

public class Example
{
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        // Create a dynamic assembly. In Silverlight, dynamic assemblies have only
        // one module, and are always created with AssemblyBuilderAccess.Run.
        //
        AssemblyName asmname = new AssemblyName("temp");
        AssemblyBuilder asmbuild = 
            AppDomain.CurrentDomain.DefineDynamicAssembly(asmname, 
                                                          AssemblyBuilderAccess.Run);
        ModuleBuilder modbuild = asmbuild.DefineDynamicModule("temp");

        // Define a type. If TypeAttributes.Public is not specified, the default
        // access is private, and instances of the type cannot be created.
        //
        TypeBuilder typebuild1 = modbuild.DefineType("TestType", TypeAttributes.Public);

        // Create the type, and create an instance of the type.
        //
        Type t = typebuild1.CreateType();
        Object obj = Activator.CreateInstance(t);

        outputBlock.Text += t.AssemblyQualifiedName + "\n";
        outputBlock.Text += obj.ToString();
   }
}

/* This example produces the following output:

TestType, temp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
TestType
 */

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.