我如何获得一个数组字段的字段信息?
-
10-07-2019 - |
题
我试图从一个结构内获得一个数组值的字段信息。到目前为止,我有以下的,但我没有看到如何得到我想要的infomration。
[StructLayout(LayoutKind.Sequential)]
public struct Test
{
public byte Byte1;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=3)]
public Test2[] Test1;
}
BindingFlags struct_field_flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
FieldInfo[] all_struct_fields = typeof(Test).GetFields(struct_field_flags);
foreach (FieldInfo struct_field in all_struct_fields)
{
if(struct_field.FieldType.IsArray)
{
// Get FieldInfo for each value in the Test1 array within Test structure
}
}
所以,如果我做的:
Type array_type = struct_field.FieldType.GetElementType();
这将返回的Test2类型,但我不想要的阵列的类型,我想要的结构的字段信息或字段,以便可以通过在其内设置的值。
解决方案
很抱歉的初始错误的答案。我才懒得创建自己的Test2类型,所以我用一个字符串代替。这里的正确答案(希望):
我没有你想用下面的代码做什么:
class Program
{
static void Main(string[] args)
{
object sampleObject = GetSampleObject();
FieldInfo[] testStructFields = typeof(Test).GetFields();
foreach (FieldInfo testStructField in testStructFields)
{
if (testStructField.FieldType.IsArray)
{
// We can cast to ILIst because arrays implement it and we verfied that it is an array in the if statement
System.Collections.IList sampleObject_test1 = (System.Collections.IList)testStructField.GetValue(sampleObject);
// We can now get the first element of the array of Test2s:
object sampleObject_test1_Element0 = sampleObject_test1[0];
// I hope this the FieldInfo that you want to get:
FieldInfo myValueFieldInfo = sampleObject_test1_Element0.GetType().GetField("MyValue");
// Now it is possible to read and write values
object sampleObject_test1_Element0_MyValue = myValueFieldInfo.GetValue(sampleObject_test1_Element0);
Console.WriteLine(sampleObject_test1_Element0_MyValue); // prints 99
myValueFieldInfo.SetValue(sampleObject_test1_Element0, 55);
sampleObject_test1_Element0_MyValue = myValueFieldInfo.GetValue(sampleObject_test1_Element0);
Console.WriteLine(sampleObject_test1_Element0_MyValue); // prints 55
}
}
}
static object GetSampleObject()
{
Test sampleTest = new Test();
sampleTest.Test1 = new Test2[5];
sampleTest.Test1[0] = new Test2() { MyValue = 99 };
object sampleObject = sampleTest;
return sampleObject;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct Test2
{
public int MyValue;
}
[StructLayout(LayoutKind.Sequential)]
public struct Test
{
public byte Byte1;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public Test2[] Test1;
}
这是最重要的行:
FieldInfo myValueFieldInfo = sampleObject_test1_Element0.GetType().GetField("MyValue");
这应该给你,你正在谈论的字段信息。
其他提示
究竟是什么,你以后?这里的是的无FieldInfo
为阵...你可以通过获取阵列(如Array
)迭代值,并在项目迭代...只是使用:
Array arr = (Array)field.GetValue(obj);
与@ weiqure的技术的问题是,如果在阵列已经具有在它的至少一种元素,它才有效。下面是一个方法来找到该数组元素类型,是否含有元素或不:
bool GetArrayElementType(FieldInfo field, out Type elementType)
{
if (field.FieldType.IsArray && field.FieldType.FullName.EndsWith("[]"))
{
string fullName = field.FieldType.FullName.Substring(0, field.FieldType.FullName.Length - 2);
elementType = Type.GetType(string.Format("{0},{1}", fullName, field.FieldType.Assembly.GetName().Name));
return true;
}
elementType = null;
return false;
}
这是你将如何使用该功能:
void Test(object targetObject, string fieldName)
{
FieldInfo field = targetObject.GetType().GetField(fieldName);
Type elementType;
bool success = GetArrayElementType(field, out elementType);
}
不隶属于 StackOverflow