ElementCount is read-only

>>UnmanagedMarshal um = UnmanagedMarshal.DefineLPArray(typeof
(int));
>>um.ElementCount = count;
>>ParameterBuilder.SetMarshal(em);

Here is my code:

string baseName = "FooBarLib";

AssemblyName asmName = new AssemblyName();
asmName.Name = baseName;

//create a persistable dynamic persistable assembly
AssemblyBuilder asmbld =
AppDomain.CurrentDomain.DefineDynamicAssembly
(asmName,AssemblyBuilderAccess.Save);

//create a single module
string moduleName = baseName + ".dll";
ModuleBuilder module = asmbld.DefineDynamicModule
(moduleName,moduleName);

//create an interface definition for ITester
TypeBuilder typeBld =  module.DefineType(asmName
+ ".ITester",TypeAttributes.Interface|TypeAttributes.Abstract
);

//add a single method definition:
//      void SetArray( int count, [MarshalAs
(UnmanagedType.LPArray, SizeParamIndex=0)] int[] array);
Type[] paramTypes = { typeof(System.Int32), typeof
(System.Int32[]) };
Type returnType = typeof(void);
MethodAttributes mattr =
MethodAttributes.Abstract|MethodAttributes.NewSlot|MethodAttr
ibutes.HideBySig|

        MethodAttributes.Virtual|MethodAttributes.Public;

MethodBuilder methodBld = typeBld.DefineMethod("SetArray",
        mattr,
        CallingConventions.HasThis|CallingConventions.Standar
d,
        returnType,paramTypes);
methodBld.SetImplementationFlags
(MethodImplAttributes.InternalCall|MethodImplAttributes.Runti
me);

//add more info for the two parameters ( direction and param
names and marshal attribute)
ParameterBuilder paraBld = methodBld.DefineParameter
(1,ParameterAttributes.In,"count");

paraBld = methodBld.DefineParameter
(2,ParameterAttributes.In|ParameterAttributes.HasFieldMarshal
,"array");
paraBld.SetMarshal(UnmanagedMarshal.DefineLPArray
(UnmanagedType.LPArray));

//finalize the type
typeBld.CreateType();

//save the assembly to file system
asmbld.Save( moduleName );

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 26 Jan 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to