-1

我有一个类,它有几个普通类型的元素,如 int、String 等。它还有几个元素是其他类的各种列表,可以是空的或有 1 到多个项目。

我有一个使用父类的泛型类型调用的函数,我想分析可能在子元素中的数据,而不知道类型。

我正在使用以下代码获取父成员:

var getProperty = System.Runtime.CompilerServices.
                  CallSite<Func<System.Runtime.CompilerServices.CallSite, 
              object, object>>
                 .Create(Microsoft.CSharp.RuntimeBinder.
                  Binder.GetMember(0, property.Name, thisObject.GetType(), new[] 
                  {
                      Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create(0, null)
                  }));

var thisValue = getProperty.Target(getProperty, thisObject);

我将值放入 var thisValue 中。此时如果我确定 thisValue 的底层类型是列表类型,那么如何获取列表内容的类型呢?

这是实际功能....我似乎无法很好地格式化它。

        public static bool ObjectIsLike<T>(this T thisObject, T compareObject, params object[] argumentsToExclude)
    {
        for (int counter = 0; counter < argumentsToExclude.Length - 1; counter++)
        {
            argumentsToExclude[counter] = argumentsToExclude[counter].ToString().ToUpper();
        }
        bool objectIsLike = true;
        foreach (var property in thisObject.GetType().GetProperties())
        {
            string fieldName = property.Name;

            if (!argumentsToExclude.Contains(fieldName.ToUpper()))
            {
                try
                {
                    var getProperty = System.Runtime.CompilerServices.CallSite<Func<System.Runtime.CompilerServices.CallSite, object, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.GetMember(0, property.Name, thisObject.GetType(), new[] { Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create(0, null) }));
                    var thisValue = getProperty.Target(getProperty, thisObject);
                    getProperty = System.Runtime.CompilerServices.CallSite<Func<System.Runtime.CompilerServices.CallSite, object, object>>.Create(Microsoft.CSharp.RuntimeBinder.Binder.GetMember(0, property.Name, compareObject.GetType(), new[] { Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create(0, null) }));
                    var compareValue = getProperty.Target(getProperty, compareObject);
                    if (!(compareValue == null && thisValue == null))
                    {
                        if (compareValue == null || thisValue == null)
                            objectIsLike = false;
                        else
                            if (compareValue.GetType().FullName.Contains("List"))
                            {
                                //Ignore Lists
                            }
                            else
                                if (!compareValue.Equals(thisValue))
                                    {
                                        objectIsLike = false;
                                    }
                    }
                }
                catch
                {
                    objectIsLike = false;
                }
            }
        }
        return objectIsLike;
    }
4

1 回答 1

0

GetType() 会为你工作吗?类程序 { 静态无效 Main(string[] args) { MyClass1 c1 = new MyClass1();

        foreach (var s in c1.pp)
        {
            Console.WriteLine(s.GetType());    
        }
        Console.Read();
    }


}


public class MyClass1
{
    public MyClass2 p;
    public List<object> pp;

    public MyClass1()
    {
        p = new MyClass2();
        pp = new List<object>();
        pp.Add(new MyClass2());
        pp.Add(new MyClass3());
        pp.Add(new MyClass4());
    }
}



public class MyClass2
{
    public List<object> ppp;

    public MyClass2()
    {
        ppp = new List<object>();
        ppp.Add(new MyClass3());
        ppp.Add(new MyClass4());
    }
}

public class MyClass3
{
    public int v;
}

public class MyClass4
{
    public int v;
}
于 2013-11-06T17:18:28.673 回答