How to inject a struct with UnmanagedType field? #893
Unanswered
13316642000
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
can anyone tell me how to inject a struct like this
public struct test
{
public int cbS;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
public string Path;
}
my code is:
var test = new TypeDefinition("namespace_1", "test", TypeAttributes.Sealed | TypeAttributes.SequentialLayout | TypeAttributes.Public |
TypeAttributes.BeforeFieldInit, hMainMod.ImportReference(typeof(System.ValueType)));
var test_cb = new FieldDefinition("cb", Mono.Cecil.FieldAttributes.Public, hMainMod.TypeSystem.Int32);
var test_Path = new FieldDefinition("Path", Mono.Cecil.FieldAttributes.Public, hMainMod.TypeSystem.String);
hMainMod.Types.Add(test);
var FixedAttr = new FixedSysStringMarshalInfo();
FixedAttr.NativeType = NativeType.FixedSysString;
FixedAttr.Size = 255;
test_Path.DeclaringType = test;
test_Path.Attributes = Mono.Cecil.FieldAttributes.Assembly | Mono.Cecil.FieldAttributes.PInvokeImpl;
test_Path.InitialValue = new Byte[0];
test.Fields.Add(test_cb);
test.Fields.Add(test_Path);
the IL is:
.class public sequential ansi sealed beforefieldinit WindowsApplication1.test
extends [mscorlib]System.ValueType
{
// Fields
.field public int32 cb
.field public marshal(fixed sysstring[255]) string Path
} // end of class WindowsApplication1.test
but the IL not work well ,can anyone help me?
Beta Was this translation helpful? Give feedback.
All reactions