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

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

FieldBuilder.SetMarshal メソッド

メモ : このメソッドは、互換性のために残されています。

フィールドネイティブ マーシャリング記述します

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

<ObsoleteAttribute("An alternate API is available: Emit the
 MarshalAs custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")>
 _
Public Sub SetMarshal ( _
    unmanagedMarshal As UnmanagedMarshal _
)
Dim instance As FieldBuilder
Dim unmanagedMarshal As UnmanagedMarshal

instance.SetMarshal(unmanagedMarshal)
[ObsoleteAttribute("An alternate API is available: Emit the MarshalAs custom
 attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")]
 
public void SetMarshal (
    UnmanagedMarshal unmanagedMarshal
)
[ObsoleteAttribute(L"An alternate API is available: Emit the MarshalAs custom
 attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")]
 
public:
void SetMarshal (
    UnmanagedMarshal^ unmanagedMarshal
)
/** @attribute ObsoleteAttribute("An alternate API is available: Emit the MarshalAs
 custom attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")
 */ 
public void SetMarshal (
    UnmanagedMarshal unmanagedMarshal
)
ObsoleteAttribute("An alternate API is available: Emit the MarshalAs custom
 attribute instead. http://go.microsoft.com/fwlink/?linkid=14202")
 
public function SetMarshal (
    unmanagedMarshal : UnmanagedMarshal
)

パラメータ

unmanagedMarshal

このフィールドネイティブ マーシャリング指定する記述子

例外例外
例外種類条件

ArgumentNullException

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

InvalidOperationException

外側の型が CreateType を使用して作成されています。

解説解説

SetMarshal使用方法については、次のコード例参照してください

Imports System
Imports System.Runtime.InteropServices
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security.Permissions
Imports System.Runtime.CompilerServices

Public Class FieldBuilder_Sample

   Public Shared Function
 CreateType(ByVal currentDomain As AppDomain)
 As Type

      ' Create an assembly.
      Dim myAssemblyName As New
 AssemblyName()
      myAssemblyName.Name = "DynamicAssembly"
      Dim myAssembly As AssemblyBuilder = currentDomain.DefineDynamicAssembly(myAssemblyName,
 _
                                          AssemblyBuilderAccess.RunAndSave)
      ' Create a dynamic module in Dynamic Assembly.
      Dim myModuleBuilder As ModuleBuilder
 = myAssembly.DefineDynamicModule("MyModule", _
                                            "MyModule.mod")
      ' Define a public class named "MyClass" in the assembly.
      Dim myTypeBuilder As TypeBuilder = myModuleBuilder.DefineType("MyClass",
 _
                                         TypeAttributes.Public)
      Dim myTypeBuilder2 As TypeBuilder = myModuleBuilder.DefineType("MyClass2",
 _
         TypeAttributes.Public Or TypeAttributes.BeforeFieldInit
 Or _
         TypeAttributes.SequentialLayout Or TypeAttributes.AnsiClass
 Or TypeAttributes.Sealed)
      Dim myFieldBuilder1 As FieldBuilder =
 myTypeBuilder2.DefineField("myBytes1", _
         GetType(Byte), FieldAttributes.Public)
      Dim myFieldBuilder2 As FieldBuilder =
 myTypeBuilder2.DefineField("myBytes2", _
         GetType(Byte), FieldAttributes.Public)
      Dim myFieldBuilder3 As FieldBuilder =
 myTypeBuilder2.DefineField("myErrorCode", _
         GetType(Short), FieldAttributes.Public)
      Dim myFieldBuilder4 As FieldBuilder =
 myTypeBuilder2.DefineField("myReserved1", _
         GetType(Short), FieldAttributes.Public)
      Dim myFieldBuilder5 As FieldBuilder =
 myTypeBuilder2.DefineField("myReserved2", _
         GetType(Short), FieldAttributes.Public)
      Dim myFieldBuilder6 As FieldBuilder =
 myTypeBuilder2.DefineField("myPathName", _
         GetType(Char()), FieldAttributes.Public)
      myFieldBuilder6.SetMarshal(UnmanagedMarshal.DefineByValArray(128))
      myFieldBuilder6.SetOffset(4)
      Dim myType1 As Type = myTypeBuilder2.CreateType()
      ' Create the PInvoke method for 'OpenFile' method of 'Kernel32.dll'.
      Dim myParameters As Type() = {GetType(String),
 myType1, GetType(System.UInt32)}
      Dim myMethodBuilder As MethodBuilder
 = myTypeBuilder.DefinePInvokeMethod("OpenFile",
 _
         "kernel32.dll", MethodAttributes.Public Or
 MethodAttributes.Static Or _
         MethodAttributes.HideBySig, CallingConventions.Standard, GetType(IntPtr),
 _
         myParameters, CallingConvention.Winapi, CharSet.None)
      Dim myAttributeType As Type = GetType(MethodImplAttribute)
      Dim myConstructorInfo As ConstructorInfo
 = myAttributeType.GetConstructor(New Type(0) _
         {GetType(MethodImplOptions)})
      Dim myAttributeBuilder As New
 CustomAttributeBuilder(myConstructorInfo, _
         New Object() {MethodImplOptions.PreserveSig})
      myMethodBuilder.SetCustomAttribute(myAttributeBuilder)
      Dim myParameterBuilder2 As ParameterBuilder
 = myMethodBuilder.DefineParameter(2, _
         ParameterAttributes.Out, "myClass2")
      Dim myType As Type = myTypeBuilder.CreateType()
      myAssembly.Save("EmittedAssembly.dll")
      Return myType
   End Function 'CreateType

   <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")>
 _
   Public Shared Sub Main()
      Try
         Dim myType As Type = CreateType(Thread.GetDomain())
         Dim myClass2 As Type = myType.Module.GetType("MyClass2")
         Dim myParam2 As Object
 = Activator.CreateInstance(myClass2)
         Dim myUint As System.UInt32
         myUint.Parse("800")

         Dim myArgs As Object()
 = {"MyFile.Txt", myParam2, myUint}
         Dim myObject As Object
 = myType.InvokeMember("OpenFile", _
                                    BindingFlags.Public Or BindingFlags.InvokeMethod
 Or _
                                    BindingFlags.Static, Nothing,
 Nothing, myArgs)
         Console.WriteLine("MyClass.OpenFile method returned: '{0}'",
 myObject)
      Catch e As Exception
         Console.WriteLine("Exception Caught: " &
 e.Message)
      End Try
   End Sub 'Main
End Class 'FieldBuilder_Sample
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Permissions;
using System.Runtime.CompilerServices;

public class FieldBuilder_Sample
{
   public static Type CreateType(AppDomain
 currentDomain)
   {
      // Create an assembly.
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "DynamicAssembly";
      AssemblyBuilder myAssembly =
         currentDomain.DefineDynamicAssembly(myAssemblyName,AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in Dynamic Assembly.
      ModuleBuilder myModuleBuilder=myAssembly.DefineDynamicModule("MyModule"
,"MyModule.mod");
      // Define a public class named "MyClass" in the assembly.
      TypeBuilder myTypeBuilder= myModuleBuilder.DefineType("MyClass",TypeAttributes.Public);
      TypeBuilder myTypeBuilder2 = myModuleBuilder.DefineType("MyClass2"
,
         TypeAttributes.Public|TypeAttributes.BeforeFieldInit|TypeAttributes.SequentialLayout|TypeAttributes.AnsiClass|TypeAttributes.Sealed);
      FieldBuilder myFieldBuilder1= myTypeBuilder2.DefineField("myBytes1"
,
                                    typeof(byte),FieldAttributes.Public);
      FieldBuilder myFieldBuilder2= myTypeBuilder2.DefineField("myBytes2"
,
                                    typeof(byte),FieldAttributes.Public);
      FieldBuilder myFieldBuilder3= myTypeBuilder2.DefineField("myErrorCode"
,
                                    typeof(short),FieldAttributes.Public);
      FieldBuilder myFieldBuilder4= myTypeBuilder2.DefineField("myReserved1"
,
                                    typeof(short),FieldAttributes.Public);
      FieldBuilder myFieldBuilder5= myTypeBuilder2.DefineField("myReserved2"
,
                                    typeof(short),FieldAttributes.Public);
      FieldBuilder myFieldBuilder6= myTypeBuilder2.DefineField("myPathName"
,
                                    typeof(char[]),FieldAttributes.Public);
      myFieldBuilder6.SetMarshal(UnmanagedMarshal.DefineByValArray(128)); 
      myFieldBuilder6.SetOffset(4);
      Type myType1 = myTypeBuilder2.CreateType();  
      // Create the PInvoke method for 'OpenFile' method of 'Kernel32.dll'.
      Type[] myParameters={ typeof(string), myType1 ,typeof(uint)};
 
      MethodBuilder myMethodBuilder= myTypeBuilder.DefinePInvokeMethod("OpenFile"
,
                                     "kernel32.dll",MethodAttributes.Public|MethodAttributes.Static|MethodAttributes.HideBySig
,
                                       CallingConventions.Standard,typeof(IntPtr)
,
                                       myParameters,CallingConvention.Winapi,CharSet.None);
      Type myAttributeType = typeof(MethodImplAttribute);
      ConstructorInfo myConstructorInfo = 
         myAttributeType.GetConstructor(new Type[1]{typeof(MethodImplOptions)});
      CustomAttributeBuilder myAttributeBuilder = new CustomAttributeBuilder(myConstructorInfo
,
                                                   new object[1]{MethodImplOptions.PreserveSig});
      myMethodBuilder.SetCustomAttribute(myAttributeBuilder);
      ParameterBuilder myParameterBuilder2=myMethodBuilder.DefineParameter(2,
                                            ParameterAttributes.Out,"myClass2");
      Type myType=myTypeBuilder.CreateType();
      myAssembly.Save("EmittedAssembly.dll");
      return myType;
   }

   [PermissionSetAttribute(SecurityAction.Demand, Name="FullTrust")]
   public static void Main()
   {
      try
      {
         Type myType = CreateType(Thread.GetDomain());
         Type myClass2 = myType.Module.GetType("MyClass2"); 
         object myParam2 = Activator.CreateInstance(myClass2);
         uint myUint=0x00000800;
         object[] myArgs= {"MyFile.Txt",myParam2,myUint};
         Object myObject  = myType.InvokeMember("OpenFile",BindingFlags.Public
 | 
            BindingFlags.InvokeMethod | BindingFlags.Static,null
,null,myArgs);
         Console.WriteLine("MyClass.OpenFile method returned: \"{0}\"",
 myObject);
      }
      catch(Exception e)
      {
         Console.WriteLine("Exception Caught "+e.Message);
      }
   }

}
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::CompilerServices;
Type^ CreateType( AppDomain^ currentDomain )
{
   // Create an assembly.
   AssemblyName^ myAssemblyName = gcnew AssemblyName;
   myAssemblyName->Name = "DynamicAssembly";
   AssemblyBuilder^ myAssembly = currentDomain->DefineDynamicAssembly( myAssemblyName,
 AssemblyBuilderAccess::RunAndSave );

   // Create a dynamic module in Dynamic Assembly.
   ModuleBuilder^ myModuleBuilder = myAssembly->DefineDynamicModule( "MyModule",
 "MyModule.mod" );

   // Define a public class named S"MyClass" in the assembly.
   TypeBuilder^ myTypeBuilder = myModuleBuilder->DefineType( "MyClass",
 TypeAttributes::Public );
   TypeBuilder^ myTypeBuilder2 = myModuleBuilder->DefineType( "MyClass2",
 static_cast<TypeAttributes>(TypeAttributes::Public | TypeAttributes::BeforeFieldInit
 | TypeAttributes::SequentialLayout | TypeAttributes::AnsiClass | TypeAttributes::Sealed) );
   FieldBuilder^ myFieldBuilder1 = myTypeBuilder2->DefineField( "myBytes1",
 Byte::typeid, FieldAttributes::Public );
   FieldBuilder^ myFieldBuilder2 = myTypeBuilder2->DefineField( "myBytes2",
 Byte::typeid, FieldAttributes::Public );
   FieldBuilder^ myFieldBuilder3 = myTypeBuilder2->DefineField( "myErrorCode",
 short::typeid, FieldAttributes::Public );
   FieldBuilder^ myFieldBuilder4 = myTypeBuilder2->DefineField( "myReserved1",
 short::typeid, FieldAttributes::Public );
   FieldBuilder^ myFieldBuilder5 = myTypeBuilder2->DefineField( "myReserved2",
 short::typeid, FieldAttributes::Public );
   FieldBuilder^ myFieldBuilder6 = myTypeBuilder2->DefineField( "myPathName",
 array<char>::typeid,FieldAttributes::Public );
   myFieldBuilder6->SetMarshal( UnmanagedMarshal::DefineByValArray( 128 ) );
   myFieldBuilder6->SetOffset( 4 );
   Type^ myType1 = myTypeBuilder2->CreateType();

   // Create the PInvoke method for 'OpenFile' method of 'Kernel32.dll'.
   array<Type^>^myParameters = {String::typeid,myType1,UInt32::typeid};
   MethodBuilder^ myMethodBuilder = myTypeBuilder->DefinePInvokeMethod( "OpenFile",
 "kernel32.dll", static_cast<MethodAttributes>(MethodAttributes::Public
 | MethodAttributes::Static | MethodAttributes::HideBySig), CallingConventions::Standard, IntPtr::typeid, myParameters, CallingConvention::Winapi, CharSet::None );
   Type^ myAttributeType = MethodImplAttribute::typeid;
   array<Type^>^type1 = {MethodImplOptions::typeid};
   ConstructorInfo^ myConstructorInfo = myAttributeType->GetConstructor( type1
 );
   array<Object^>^obj1 = {MethodImplOptions::PreserveSig};
   CustomAttributeBuilder^ myAttributeBuilder = gcnew CustomAttributeBuilder( myConstructorInfo,obj1
 );
   myMethodBuilder->SetCustomAttribute( myAttributeBuilder );
   ParameterBuilder^ myParameterBuilder2 = myMethodBuilder->DefineParameter( 2,
 ParameterAttributes::Out, "myClass2" );
   Type^ myType = myTypeBuilder->CreateType();
   myAssembly->Save( "EmittedAssembly.dll" );
   return myType;
}

int main()
{
   try
   {
      Type^ myType = CreateType( Thread::GetDomain() );
      Type^ myClass2 = myType->Module->GetType( "MyClass2" );
      Object^ myParam2 = Activator::CreateInstance( myClass2 );
      UInt32 myUint = 0x00000800;
      array<Object^>^myArgs = {"MyFile.Txt",myParam2,myUint};
      Object^ myObject = myType->InvokeMember( "OpenFile", static_cast<BindingFlags>(BindingFlags::Public
 | BindingFlags::InvokeMethod | BindingFlags::Static), nullptr, nullptr, myArgs
 );
      Console::WriteLine( "MyClass::OpenFile method returned: \"{0}\"",
 myObject );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception Caught {0}", e->Message );
   }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
FieldBuilder クラス
FieldBuilder メンバ
System.Reflection.Emit 名前空間



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

辞書ショートカット

すべての辞書の索引

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

   

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



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

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

©2024 GRAS Group, Inc.RSS