I have a struct for concat string datas.
public struct MyClass1
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
public string Name;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
public string SurName;
}
I set the value struct fields.
MyStruct mystr = new MyStruct();
mystr.Name = 'John';
mystr.SurName = 'Smith';
I want to concat this string by using Marshaling and get the value as
"John Smith"
. Is it possible using marshaling ?
Use reflection instead of marshalling.
var myClass = new MyClass1 { Name = "John", SurName = "Smith" };
Console.WriteLine(MyMarshal(myClass));
static string MyMarshal(object item)
{
var values = new List<object>();
foreach (var field in item.GetType().GetFields())
{
values.Add(field.GetValue(item));
}
return string.Join(" ", values.ToArray());
}
public struct MyClass1
{
public string Name;
public string SurName;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.