简体   繁体   English

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

[英]Object of type 'System.Reflection.ParameterInfo' cannot be converted to type 'System.Int32'

I'm trying to write an extension method for objects that will dump the structure of the object to the debug output. 我正在尝试为对象编写扩展方法,该方法会将对象的结构转储到调试输出。 I'm running in to a problem when the propertyInfo is indexed (GetIndexParameters().Length >0). 在索引propertyInfo(GetIndexParameters()。Length> 0)时遇到问题。

I've tried using the following: 我尝试使用以下方法:

object value = propInfo.GetValue(myObject, propInfo.GetIndexParameteres());

this results in the following runtime error: 这将导致以下运行时错误:

  • Object of type 'System.Reflection.ParameterInfo' cannot be converted to type 'System.Int32' 无法将类型为“ System.Reflection.ParameterInfo”的对象转换为类型为“ System.Int32”的对象

Anyone got any ideas? 任何人有任何想法吗? The full code for the method is below: 该方法的完整代码如下:

[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();
    }
}

It's bad idea to dump indexer properties. 转储索引器属性是个坏主意。 It's similar to "dump method". 它类似于“转储方法”。 It's just imposible. 这是不可能的。

Also consider using PropertyInfo.CanRead before trying to get value. 在尝试获取值之前,还应考虑使用PropertyInfo.CanRead。

You are essentially trying to access SomeProp[foo, bar...]... so; 您本质上是在尝试访问SomeProp [foo,bar ...] ...因此; what are sensible index values? 什么是明智的指数值? For integers, maybe 0,0,0... is safe - maybe not. 对于整数,也许0,0,0 ...是安全的-也许不是。 It depends on the context. 这取决于上下文。 Personally, I'm not sure that this is the best way to go; 就我个人而言,我不确定这是否是最好的方法。 I might look at IList on the main object, but other than that - just look at regular properties... 我可能会查看主要对象上的IList ,但IList -只是查看常规属性...

暂无
暂无

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

相关问题 类型&#39;System.Int16&#39;的对象不能转换为类型&#39;System.Nullable`1 [System.Int32] - Object of type 'System.Int16' cannot be converted to type 'System.Nullable`1[System.Int32] “System.Int32”类型的对象无法转换为“System.UInt32&”类型 - Object of type 'System.Int32' cannot be converted to type 'System.UInt32&' Expression.Convert:'System.Int64'类型的对象无法转换为'System.Int32'类型 - Expression.Convert: Object of type 'System.Int64' cannot be converted to type 'System.Int32' 无法将类型为“ System.Double”的对象转换为类型为“ System.Int32”的对象 - Object of type 'System.Double' cannot be converted to type 'System.Int32' ASP.NET FormView:“类型&#39;System.Int32&#39;的对象不能转换为类型&#39;System.String” - ASP.NET FormView: “Object of type 'System.Int32' cannot be converted to type 'System.String” “System.Int32”类型的对象无法转换为“System.Web.Security.Cryptography.Purpose”类型 - Object of type 'System.Int32' cannot be converted to type 'System.Web.Security.Cryptography.Purpose' 无法将类型为“ System.Int32”的对象转换为类型为“ System.Reflection.RuntimePropertyInfo” - Unable to cast object of type 'System.Int32' to type 'System.Reflection.RuntimePropertyInfo' Linq和Equality Operator:类型'System.Int32'的表达式不能用于'System.Object'类型的参数 - Linq and the Equality Operator: Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' 类型'System.Int32'的表达式不能用于返回类型'System.Object' - Expression of type 'System.Int32' cannot be used for return type 'System.Object' 类型'System.Int32'的表达式不能用于方法'Boolean Equals(System.Object)'的'System.Object'类型的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM