C# 类型为'的对象;系统.反射.参数信息';无法转换为类型';System.Int32';
我试图为对象编写一个扩展方法,将对象的结构转储到调试输出。当为propertyInfo编制索引(GetIndexParameters().Length>0)时,我遇到了一个问题 我尝试使用以下方法:C# 类型为'的对象;系统.反射.参数信息';无法转换为类型';System.Int32';,c#,oop,C#,Oop,我试图为对象编写一个扩展方法,将对象的结构转储到调试输出。当为propertyInfo编制索引(GetIndexParameters().Length>0)时,我遇到了一个问题 我尝试使用以下方法: object value = propInfo.GetValue(myObject, propInfo.GetIndexParameteres()); 这将导致以下运行时错误: “System.Reflection.ParameterInfo”类型的对象无法转换为“System.Int32”类型
object value = propInfo.GetValue(myObject, propInfo.GetIndexParameteres());
这将导致以下运行时错误:
- “System.Reflection.ParameterInfo”类型的对象无法转换为“System.Int32”类型
[System.Diagnostics.Conditional("DEBUG")]
public static void DebugObject(this object debugObject)
{
System.Diagnostics.Debug.WriteLine("Debugging object: " + debugObject.GetType().Namespace);
System.Diagnostics.Debug.WriteLine(String.Format("> {0}", debugObject.GetType()));
System.Diagnostics.Debug.Indent();
try
{
if (debugObject.GetType().IsArray)
{
object[] array = ((object[])debugObject);
for (int index = 0; index < array.Length; index++)
{
System.Diagnostics.Debug.WriteLine(String.Format("- {{0}} = [{1}]", index, array[index]));
}
return;
}
object value = null;
foreach (System.Reflection.PropertyInfo propInfo in debugObject.GetType().GetProperties())
{
try
{
if (propInfo.IsIndexed())
{
System.Diagnostics.Debug.WriteLine(propInfo.ReflectedType.IsArray + " is indexed");
// THIS IS WHERE IT CHOKES. As an example, try sending in something of type System.Net.Mail.MailMessage;
value = propInfo.GetValue(debugObject, propInfo.GetIndexParameters());
}
else
{
value = propInfo.GetValue(debugObject, null);
}
if (value != null)
{
System.Diagnostics.Debug.WriteLine(String.Format("> {0} = [{1}]", propInfo.Name, value));
if (
(value.GetType() != typeof(string))
&&
(value.GetType() != typeof(int))
)
{
value.DebugObject();
}
}
}
catch (System.Reflection.TargetParameterCountException tpce)
{
System.Diagnostics.Debug.WriteLine(
String.Format(
"> Could not run GetValue for {1} (type '{0}', '{2}') because of incorrect prarmeters",
propInfo.GetType().ToString(),
propInfo.Name,
propInfo.PropertyType.Namespace
)
);
}
}
}
finally
{
System.Diagnostics.Debug.Unindent();
}
}
[System.Diagnostics.Conditional(“调试”)]
公共静态void DebugObject(此对象DebugObject)
{
System.Diagnostics.Debug.WriteLine(“调试对象:+debugObject.GetType().Namespace”);
System.Diagnostics.Debug.WriteLine(String.Format(“>{0}”,debugObject.GetType());
System.Diagnostics.Debug.Indent();
尝试
{
if(debugObject.GetType().IsArray)
{
对象[]数组=((对象[])调试对象);
for(int index=0;index{0}=[{1}]”,propInfo.Name,value));
如果(
(value.GetType()!=typeof(字符串))
&&
(value.GetType()!=typeof(int))
)
{
value.DebugObject();
}
}
}
捕获(System.Reflection.TargetParameterCountException tpce)
{
System.Diagnostics.Debug.WriteLine(
字符串格式(
“>无法运行{1}(类型{0},{2}')的GetValue,因为prarmeters不正确”,
propInfo.GetType().ToString(),
名称,
propInfo.PropertyType.Namespace
)
);
}
}
}
最后
{
System.Diagnostics.Debug.unident();
}
}
您实际上是在尝试访问SomeProp[foo,bar…]。。。所以什么是合理的索引值?对于整数,可能是0,0,0。。。是安全的——也许不是。这取决于上下文。就我个人而言,我不确定这是最好的方式;我可能会查看主对象上的IList
,但除此之外-只需查看常规属性…转储索引器属性是个坏主意。它类似于“转储方法”。这是不可能的
在尝试获取值之前,也要考虑使用PrimeTyf.CopRead。
谢谢你的回答,当我有更多的空闲时间时,我会有一个更好的外观,但是现在我要避免它。