TypeBuilder.DefineUninitializedData メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > TypeBuilder.DefineUninitializedData メソッドの意味・解説 

TypeBuilder.DefineUninitializedData メソッド

移植可能な実行可能 (PE) ファイルの .sdata セクション初期化されていないデータ フィールド定義します

名前空間: System.Reflection.Emit
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Function DefineUninitializedData ( _
    name As String, _
    size As Integer, _
    attributes As FieldAttributes _
) As FieldBuilder
Dim instance As TypeBuilder
Dim name As String
Dim size As Integer
Dim attributes As FieldAttributes
Dim returnValue As FieldBuilder

returnValue = instance.DefineUninitializedData(name, size, attributes)
public FieldBuilder DefineUninitializedData (
    string name,
    int size,
    FieldAttributes attributes
)
public:
FieldBuilder^ DefineUninitializedData (
    String^ name, 
    int size, 
    FieldAttributes attributes
)
public FieldBuilder DefineUninitializedData (
    String name, 
    int size, 
    FieldAttributes attributes
)
public function DefineUninitializedData (
    name : String, 
    size : int, 
    attributes : FieldAttributes
) : FieldBuilder

パラメータ

name

データ参照するために使用される名前。namenull埋め込むことはできません。

size

データ フィールドサイズ

attributes

フィールド属性

戻り値
データ参照するフィールド

例外例外
例外種類条件

ArgumentException

name長さが 0 です。

または

size が 0 以下か、0x003f0000 以上です。

ArgumentNullException

namenull 参照 (Visual Basic では Nothing) です。

InvalidOperationException

この型は、CreateType を使用して既に作成されています。

解説解説
使用例使用例

次のコード例は、DefineUninitializedData使用して動的な型に、初期化されていないデータ フィールド作成する方法示してます。

Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices


Module Example

   Sub Main()
      Dim myHelloWorldType As Type = CreateCallee(Thread.GetDomain())
      Dim myHelloWorldInstance As Object
 = Activator.CreateInstance(myHelloWorldType)
      Dim myGreetingFieldInfo As FieldInfo
 = myHelloWorldType.GetField("MyGreeting")
      Dim oval As Object
 = Activator.CreateInstance(myGreetingFieldInfo.FieldType)
      Dim myIntPtr As IntPtr = Marshal.AllocHGlobal(4)
      Dim rand As New Random()
      Dim iTempSeed As Integer
 = rand.Next()
      Dim bINITBYTE As Byte()
 = GetRandBytes(iTempSeed, 4)
      Dim intptrTemp As IntPtr = myIntPtr
      Dim j As Integer
      For j = 0 To 3
         Marshal.WriteByte(myIntPtr, bINITBYTE(j))
         myIntPtr = intptr.op_Explicit(myIntPtr.ToInt32 + 1)
      Next j
      myIntPtr = intptrTemp
      Dim oValNew As [Object] = Marshal.PtrToStructure(myIntPtr,
 myGreetingFieldInfo.FieldType)
      Marshal.FreeHGlobal(myIntPtr)

      myIntPtr = Marshal.AllocHGlobal(4)
      Dim myObj As Object
 = myGreetingFieldInfo.GetValue(myHelloWorldInstance)
      Marshal.StructureToPtr(myObj, myIntPtr, True)
      intptrTemp = myIntPtr
      Console.WriteLine("The value of 'MyGreeting'
 field : ")

      For j = 0 To 3
         Marshal.WriteByte(myIntPtr, bINITBYTE(j))
         Console.WriteLine(bINITBYTE(j))
         myIntPtr = intptr.op_Explicit(myIntPtr.ToInt32 + 1)
      Next j
   End Sub 'Main


   Private Function GetRandBytes(ByVal
 iRandSeed As Integer, ByVal
 iSize As Integer) As Byte()
      Dim barr(iSize) As Byte
      Dim randTemp As New
 Random(iRandSeed)
      randTemp.NextBytes(barr)
      Return barr
   End Function 'GetRandBytes


   ' Create the callee transient dynamic assembly.
   Private Function CreateCallee(ByVal
 myDomain As AppDomain) As Type
      ' Create a simple name for the callee assembly.
      Dim myAssemblyName As New
 AssemblyName()
      myAssemblyName.Name = "EmittedClass"

      ' Create the callee dynamic assembly.
      Dim myAssembly As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAssemblyName,
 AssemblyBuilderAccess.Run)


      ' Create a dynamic module in the callee assembly.
      Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")

      ' Define a public class named "MyHelloWorld"
      Dim myHelloWorldType As TypeBuilder =
 myModule.DefineType("MyHelloWorld", TypeAttributes.Public)

      ' Define a 'MyGreeting' field and initialize it.
      Dim myFieldBuilder As FieldBuilder =
 myHelloWorldType.DefineUninitializedData("MyGreeting",
 4, FieldAttributes.Public)

      ' Create the 'MyHelloWorld' class.
      Return myHelloWorldType.CreateType()
   End Function 'CreateCallee

End Module
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Security.Permissions;

public sealed class Example
{
   [PermissionSetAttribute(SecurityAction.Demand, Name="FullTrust")]
   public static void Main()
   {
      Type myHelloWorldType = CreateCallee(Thread.GetDomain());
      object myHelloWorldInstance =
      Activator.CreateInstance(myHelloWorldType);
      FieldInfo myGreetingFieldInfo =
      myHelloWorldType.GetField("MyGreeting");
      object oval = Activator.CreateInstance(myGreetingFieldInfo.FieldType);
      IntPtr myIntPtr = Marshal.AllocHGlobal(4);
      Random rand = new Random();
      int iTempSeed = rand.Next();
      byte[] bINITBYTE = GetRandBytes( iTempSeed, 4);
      IntPtr intptrTemp = myIntPtr;
      for ( int j = 0; j < 4; j++ )
      {
         Marshal.WriteByte( myIntPtr, bINITBYTE[j]);
         myIntPtr = (IntPtr)((int)myIntPtr + 1);
      }
      myIntPtr = intptrTemp;
      Object oValNew = Marshal.PtrToStructure( myIntPtr, myGreetingFieldInfo.FieldType);
      Marshal.FreeHGlobal( myIntPtr );

      myIntPtr = Marshal.AllocHGlobal(4);
      object myObj = myGreetingFieldInfo.GetValue(myHelloWorldInstance);
      Marshal.StructureToPtr(myObj, myIntPtr, true);
      intptrTemp = myIntPtr;
      Console.WriteLine("The value of 'MyGreeting' field : ");
      for ( int j = 0; j < 4; j++ )
      {
         Marshal.WriteByte( myIntPtr, bINITBYTE[j]);
         Console.WriteLine(bINITBYTE[j]);
         myIntPtr = (IntPtr)((int)myIntPtr + 1);
      }
   }

   private static byte[] GetRandBytes( int
 iRandSeed, int iSize )
   {
      byte[] barr = new byte[iSize];
      Random randTemp = new Random( iRandSeed );
      randTemp.NextBytes( barr );
      return barr;
   }

   // Create the callee transient dynamic assembly.
   private static Type CreateCallee(AppDomain
 myDomain)
   {
      // Create a simple name for the callee assembly.
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedClass";

      // Create the callee dynamic assembly.
      AssemblyBuilder myAssembly =
         myDomain.DefineDynamicAssembly(myAssemblyName,AssemblyBuilderAccess.Run);

      // Create a dynamic module in the callee assembly.
      ModuleBuilder myModule =
         myAssembly.DefineDynamicModule("EmittedModule");

      // Define a public class named "MyHelloWorld"
      TypeBuilder myHelloWorldType =
         myModule.DefineType("MyHelloWorld", TypeAttributes.Public);

      // Define a 'MyGreeting' field and initialize it.
      FieldBuilder myFieldBuilder =
         myHelloWorldType.DefineUninitializedData("MyGreeting",4,FieldAttributes.Public);

      // Create the 'MyHelloWorld' class.
      return(myHelloWorldType.CreateType());
   }

   private Example() {}
}
using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::InteropServices;
using namespace System::Security::Permissions;

public ref class Example
{
public:
   [SecurityPermission(SecurityAction::Demand, Flags=SecurityPermissionFlag::UnmanagedCode)]
   static void Main()
   {
      Type^ myHelloWorldType = CreateCallee( Thread::GetDomain() );
      Object^ myHelloWorldInstance = Activator::CreateInstance( myHelloWorldType
 );
      FieldInfo^ myGreetingFieldInfo = myHelloWorldType->GetField( "MyGreeting"
 );
      Object^ oval = Activator::CreateInstance( myGreetingFieldInfo->FieldType
 );
      IntPtr myIntPtr = Marshal::AllocHGlobal( 4 );
      Random^ rand = gcnew Random;
      int iTempSeed = rand->Next();
      array<Byte>^bINITBYTE = GetRandBytes( iTempSeed, 4 );
      IntPtr intptrTemp = myIntPtr;
      for ( int j = 0; j < 4; j++ )
      {
         Marshal::WriteByte( myIntPtr, bINITBYTE[ j ] );
         myIntPtr = (IntPtr)((int)myIntPtr + 1);

      }
      myIntPtr = intptrTemp;
      Object^ oValNew = Marshal::PtrToStructure( myIntPtr, myGreetingFieldInfo->FieldType
 );
      Marshal::FreeHGlobal( myIntPtr );
      myIntPtr = Marshal::AllocHGlobal( 4 );
      Object^ myObj = myGreetingFieldInfo->GetValue( myHelloWorldInstance );
      Marshal::StructureToPtr( myObj, myIntPtr, true );
      intptrTemp = myIntPtr;
      Console::WriteLine( "The value of 'MyGreeting' field : " );
      for ( int j = 0; j < 4; j++ )
      {
         Marshal::WriteByte( myIntPtr, bINITBYTE[ j ] );
         Console::WriteLine( bINITBYTE[ j ] );
         myIntPtr = (IntPtr)((int)myIntPtr + 1);

      }
   }


private:
   static array<Byte>^ GetRandBytes( int
 iRandSeed, int iSize )
   {
      array<Byte>^barr = gcnew array<Byte>(iSize);
      Random^ randTemp = gcnew Random( iRandSeed );
      randTemp->NextBytes( barr );
      return barr;
   }


   // Create the callee transient dynamic assembly.
   static Type^ CreateCallee( AppDomain^ myDomain )
   {
      
      // Create a simple name for the callee assembly.
      AssemblyName^ myAssemblyName = gcnew AssemblyName;
      myAssemblyName->Name = "EmittedClass";
      
      // Create the callee dynamic assembly.
      AssemblyBuilder^ myAssembly = myDomain->DefineDynamicAssembly( myAssemblyName,
 AssemblyBuilderAccess::Run );
      
      // Create a dynamic module in the callee assembly.
      ModuleBuilder^ myModule = myAssembly->DefineDynamicModule( "EmittedModule"
 );
      
      // Define a public class named "MyHelloWorld"
      TypeBuilder^ myHelloWorldType = myModule->DefineType( "MyHelloWorld",
 TypeAttributes::Public );
      
      // Define a 'MyGreeting' field and initialize it.
      FieldBuilder^ myFieldBuilder = myHelloWorldType->DefineUninitializedData(
 "MyGreeting", 4, FieldAttributes::Public );
      
      // Create the 'MyHelloWorld' class.
      return (myHelloWorldType->CreateType());
   }

};

int main()
{
   Example::Main();
}

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TypeBuilder クラス
TypeBuilder メンバ
System.Reflection.Emit 名前空間


このページでは「.NET Framework クラス ライブラリ リファレンス」からTypeBuilder.DefineUninitializedData メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からTypeBuilder.DefineUninitializedData メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からTypeBuilder.DefineUninitializedData メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

TypeBuilder.DefineUninitializedData メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



TypeBuilder.DefineUninitializedData メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS