Hello Marek,
I'd suggest you use the utility "ILReader" from:
http://blogs.msdn.com/yirutang/archi...07/621125.aspx
plus the .net Reflector from
http://www.aisto.com/roeder/dotnet/
The two tools can help us write codes to dynamically create
assembly/module/type. With the help of the tools, I wrote the following
code for your SimpleType2 type:
(Note: because Newsgroup system may wrap the long sentences and make it
hard for you to read my codes, I attach the .cs file to this message for
your convenience. You can download the .cs code file with outlook express
or windows mail)
public class Test
{
static TypeBuilder fixedBufTypeBui lder;
public static void Main()
{
AppDomain.Curre ntDomain.TypeRe solve += new
ResolveEventHan dler(CurrentDom ain_TypeResolve );
#region AttributeBuilde rs
// UnsafeValueType Attribute
ConstructorInfo unsafeValueType Ctor =
typeof(UnsafeVa lueTypeAttribut e).GetConstruct or(new Type[] { });
CustomAttribute Builder unsafeAttrBuild er = new
CustomAttribute Builder(unsafeV alueTypeCtor, new object[] { });
// UnverifiableCod eAttribute
ConstructorInfo unverifiableCto r =
typeof(Unverifi ableCodeAttribu te).GetConstruc tor(new Type[] { });
CustomAttribute Builder unverifiableAtt rBuilder = new
CustomAttribute Builder(unverif iableCtor, new object[] { });
// CompilerGenerat edAttribute
ConstructorInfo compilerGenCtor =
typeof(Compiler GeneratedAttrib ute).GetConstru ctor(new Type[] { });
CustomAttribute Builder compilerGenAttr Builder = new
CustomAttribute Builder(compile rGenCtor, new object[] { });
// FixedBufferAttr ibute
ConstructorInfo fixedBufCtor =
typeof(FixedBuf ferAttribute).G etConstructor(n ew Type[] { typeof(Type),
typeof(int) });
CustomAttribute Builder fixedBufAttrBui lder = new
CustomAttribute Builder(fixedBu fCtor, new object[] { typeof(double), 5 });
#endregion
AssemblyName asmname = new AssemblyName("m ytest");
AssemblyBuilder asmbuild =
AppDomain.Curre ntDomain.Define DynamicAssembly (asmname,
AssemblyBuilder Access.RunAndSa ve);
ModuleBuilder moduleBuilder =
asmbuild.Define DynamicModule(" mytest", "mytest.dll ");
moduleBuilder.S etCustomAttribu te(unverifiable AttrBuilder);
TypeBuilder typeBuilder = moduleBuilder.D efineType("Simp leType2",
TypeAttributes. Public | TypeAttributes. SequentialLayou t,
typeof(ValueTyp e), PackingSize.Siz e8);
#region <dArray>e__Fixe dBuffer0
fixedBufTypeBui lder =
typeBuilder.Def ineNestedType(" <dArray>e__Fixe dBuffer0",
TypeAttributes. NestedPublic | TypeAttributes. SequentialLayou t,
typeof(ValueTyp e), 40);
fixedBufTypeBui lder.SetCustomA ttribute(compil erGenAttrBuilde r);
fixedBufTypeBui lder.SetCustomA ttribute(unsafe AttrBuilder);
fixedBufTypeBui lder.DefineFiel d("FixedElement Field",
typeof(double), FieldAttributes .Public);
#endregion
typeBuilder.Def ineField("dScal ar1", typeof(double),
FieldAttributes .Public);
FieldBuilder fixedBufFieldBu ilder =
typeBuilder.Def ineField("dArra y", fixedBufTypeBui lder,
FieldAttributes .Public);
fixedBufFieldBu ilder.SetCustom Attribute(fixed BufAttrBuilder) ;
typeBuilder.Def ineField("iScal ar1", typeof(int),
FieldAttributes .Public);
typeBuilder.Cre ateType();
asmbuild.Save(" mytest");
}
private static Assembly CurrentDomain_T ypeResolve(obje ct sender,
ResolveEventArg s args)
{
if (args.Name == "<dArray>e__Fix edBuffer0")
{
return fixedBufTypeBui lder.CreateType ().Assembly;
}
return null;
}
}
In the code above, the line
AppDomain.Curre ntDomain.TypeRe solve += new
ResolveEventHan dler(CurrentDom ain_TypeResolve );
Is used for the nested struct type. See:
https://connect.microsoft.com/Visual...ck.aspx?Feedba
ckID=98299
Below is my detailed steps when I write the code above (I think this may
help you if you encounter such cases again in future)
Step1. Compose a simple assembly with our target code:
[StructLayout(La youtKind.Sequen tial, Pack = 8)]
public unsafe struct SimpleType2
{
public double dScalar1;
public fixed double dArray[5];
public int iScalar1;
}
Step2. Compile the assembly, use .NET Reflector to open the output dll, and
use ILReader to parse the dll.
Step3. Translate the output of ILReader to our AssemblyBuilder ,
TypeBuilder, DefineField codes
Step4. Run our AssemblyBuilder , TypeBuilder, DefineField codes and get the
resulting assembly dll.
Step5. Use .Net Reflector to open the dll, and compare its result with the
that of the dll in step2.
If you have any other concerns or questions, feel free to let me know.
Regards,
Jialiang Ge (ji****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsof t.com.
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.