Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 类型为'的对象;系统.反射.参数信息';无法转换为类型';System.Int32';_C#_Oop - Fatal编程技术网

C# 类型为'的对象;系统.反射.参数信息';无法转换为类型';System.Int32';

C# 类型为'的对象;系统.反射.参数信息';无法转换为类型';System.Int32';,c#,oop,C#,Oop,我试图为对象编写一个扩展方法,将对象的结构转储到调试输出。当为propertyInfo编制索引(GetIndexParameters().Length>0)时,我遇到了一个问题 我尝试使用以下方法: object value = propInfo.GetValue(myObject, propInfo.GetIndexParameteres()); 这将导致以下运行时错误: “System.Reflection.ParameterInfo”类型的对象无法转换为“System.Int32”类型

我试图为对象编写一个扩展方法,将对象的结构转储到调试输出。当为propertyInfo编制索引(GetIndexParameters().Length>0)时,我遇到了一个问题

我尝试使用以下方法:

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。

谢谢你的回答,当我有更多的空闲时间时,我会有一个更好的外观,但是现在我要避免它。