我想要使用反射得到一个字节[]。不幸的是,结果总是空的。该物业充满了数据。这是我的代码片段。从PropertyInfo获取字节[]返回NULL
public static void SaveFile(BusinessObject document)
{
Type boType = document.GetType();
PropertyInfo[] propertyInfo = boType.GetProperties();
Object obj = Activator.CreateInstance(boType);
foreach (PropertyInfo item in propertyInfo)
{
Type xy = item.PropertyType;
if (String.Equals(item.Name, "Content") && (item.PropertyType == typeof(Byte[])))
{
Byte[] content = item.GetValue(obj, null) as Byte[];
}
}
return true;
}
这里的工作代码:
public static void SaveFile(BusinessObject document)
{
Type boType = document.GetType();
PropertyInfo[] propertyInfo = boType.GetProperties();
foreach (PropertyInfo item in propertyInfo)
{
if (String.Equals(item.Name, "Content") && (item.PropertyType == typeof(Byte[])))
{
Byte[] content = item.GetValue(document, null) as Byte[];
}
}
}
回复BTW 2:可能的内容仅在一个派生类的属性。 –
@亨克·霍特曼:是的,情况可能如此。 –
嗨,丹尼尔。当然你是对的。我怎么会这么盲目。谢谢! – Markus