繁体   English   中英

无法将类型为“ System.Reflection.ParameterInfo”的对象转换为类型为“ System.Int32”的对象

[英]Object of type 'System.Reflection.ParameterInfo' cannot be converted to type '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();
    }
}

转储索引器属性是个坏主意。 它类似于“转储方法”。 这是不可能的。

在尝试获取值之前,还应考虑使用PropertyInfo.CanRead。

您本质上是在尝试访问SomeProp [foo,bar ...] ...因此; 什么是明智的指数值? 对于整数,也许0,0,0 ...是安全的-也许不是。 这取决于上下文。 就我个人而言,我不确定这是否是最好的方法。 我可能会查看主要对象上的IList ,但IList -只是查看常规属性...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM