0

私は次のクラスを持っています:

ユーザー

public partial class User
{
    public long iduser { get; set; }
    public string email { get; set; }
    public string name { get; set; }
    public System.DateTime birthdate { get; set; }
    public string about { get; set; }
    public bool active { get; set; }
    public System.DateTime created_date { get; set; }
    public System.DateTime last_update { get; set; }
    public string password { get; set; }
    public string image { get; set; }
    public string username { get; set; }
    public virtual ICollection<Interest> Interests { get; set; }
}

興味

public partial class Interest
{

    public long idinterest { get; set; }
    public string name { get; set; }
    public bool active { get; set; }
    public System.DateTime last_update { get; set; }
    public System.DateTime created_date { get; set; }
    public string css_class { get; set; }

    public virtual ICollection<User> Users { get; set; }
}

WSReturnUserGetById

public class WSReturnUserGetById
{
    public long iduser { get; set; }
    public string email { get; set; }
    public string name { get; set; }
    public System.DateTime birthdate { get; set; }
    public string about { get; set; }
    public List<WSReturnInterestGetById> Interests { get; set; }
}

および WSReturnInterestBetById

public class WSReturnInterestGetById
{
    public long idinterest { get; set; }
    public string name { get; set; }
    public string css_class { get; set; }
}

次のコードを使用して、User のデータを WSReturnUserGetById に入力しています。

public T PopulateObjects<T>(object obj) where T : class, new()
{
    if (obj == null) return null;
    T Obj = new T();
    PropertyInfo[] properties = obj.GetType().GetProperties();
    foreach (PropertyInfo p in properties)
    {
        PropertyInfo objPf = typeof(T).GetProperty(p.Name);
        if (objPf != null)
        {
            if (p.PropertyType == objPf.PropertyType)
            {
                objPf.SetValue(Obj, p.GetValue(obj));
            }
        }
    }
    return Obj;
}

オブジェクトのリストを作成する関数も 1 つあります。

public List PopulateObjectList(IEnumerable objects) where T: class, new() {

    List<T> response = new List<T>();
    foreach (U obj in objects)
    {
        T Obj = new T();
        if (obj != null)
        {
            PropertyInfo[] properties = obj.GetType().GetProperties();
            foreach (PropertyInfo p in properties)
            {
                PropertyInfo objPf = typeof(T).GetProperty(p.Name);
                if (objPf != null)
                {
                    if (p.PropertyType == objPf.PropertyType)
                    {
                        objPf.SetValue(Obj, p.GetValue(obj));
                    }
                }
            }
        }
        response.Add(Obj);
    }

    return response;
}

そのコードを使用すると、タイプが異なるため、User と WSReturnUserGetById のプロパティ「Interests」を除いて機能します。だから、私は調整しようとしており、この機能を使用して機能させようとしています:

public object populateCompleteObj<T, U>(U mainobj) where T : class, new()
{
    if (mainobj.GetType().IsGenericType && mainobj is IEnumerable)
    {
        List<T> response = new List<T>();
        foreach (object obj in (IEnumerable)mainobj)
        {
            T Obj = new T();
            if (obj != null)
            {
                PropertyInfo[] properties = obj.GetType().GetProperties();
                foreach (PropertyInfo p in properties)
                {
                    PropertyInfo objPf = typeof(T).GetProperty(p.Name);
                    if (objPf != null)
                    {
                        if (typeof(IEnumerable).IsAssignableFrom(objPf.PropertyType))
                        {
                            objPf.SetValue(Obj, populateCompleteObj<'objpf property class', 'obj property class'>(p.GetValue(obj)));
                        }
                        else if (p.PropertyType == objPf.PropertyType)
                        {
                            objPf.SetValue(Obj, p.GetValue(obj));
                        }
                    }
                }
            }
        }

    }
    else
    {
        if (mainobj == null) return null;
        T Obj = new T();
        PropertyInfo[] properties = mainobj.GetType().GetProperties();
        foreach (PropertyInfo p in properties)
        {
            PropertyInfo objPf = typeof(T).GetProperty(p.Name);
            if (objPf != null)
            {
                if (p.PropertyType == objPf.PropertyType)
                {
                    objPf.SetValue(Obj, p.GetValue(mainobj));
                }
            }
        }
        return Obj;
    }

}

問題は、プロパティ情報のクラスを取得する方法がわからないため、再帰を実行できることです。誰もこれを行う方法を知っていますか?

4

2 に答える 2

0

私があなたの言いたいことを理解していれば、あなたはジェネリックの内部型を取得する方法を探しています.

それはstringにありList<string>ます。

次のコードを使用してこれを実現できます。

PropertyInfo objPf = typeof(T).GetProperty(p.Name);
var type = pi.PropertyType.GetGenericArguments()[0];
于 2014-08-02T18:29:01.980 に答える