2011-04-09 69 views
1

我有一個小問題。
我的問題是以下。如何將對象反映到僅在運行時已知類型

我有兩種類型:AssetData和AssetData。它們基本相同,但不是遺傳的。
現在我已經知道類型爲「AssetData」的屬性,並且我有一個AssetData類型的對象(包含一個Texture2D對象)。

現在我想將AssetData對象轉換爲AssetData對象。因爲我不知道通用參數,所以AssetData現在具有該類型轉換的操作符,但AssetData可以轉換爲AssetData。

我試過並試過我能做的事來解決它,但我沒有更多的想法。

這是我的情況:我有一個屬性類型,我有一個AssetData具有相同的對象,但我必須將AssetData設置爲屬性 - 所以我必須設置「AssetData」,而不是AssetData 。

這是我的代碼。我無法對其進行硬編碼,因爲在添加新類型之後要更改這些內容。這是我最近的嘗試。它幾乎工程,但有投不工作的問題,因爲沒有there's的AssetData操作...

foreach (PropertyInfo pinf in comp.GetType().GetProperties()) 
      { 
       for (int i = 0; i < cdata.PInf.Count; i++) 
       { 
        if (cdata.PInf[i] != pinf.Name) continue; 

        AssetData assetData = new AssetData 
               { 
                AssetName = cdata.AN[i], 
                AssetType = Type.GetType(cdata.AT[i], true) 
               }; 
        Application.Game.GetSystem<ContentManager>().LoadContent(ref assetData); 

        if (pinf.PropertyType.IsGenericType) 
        { 
         MethodInfo method = typeof (DynamicCast).GetMethod("Cast").GetGenericMethodDefinition().MakeGenericMethod(
           assetData.AssetType); 

         Type castedAssetType = 
          pinf.PropertyType.GetGenericTypeDefinition().MakeGenericType(assetData.AssetType); 

         dynamic castedAsset = method.Invoke(typeof (DynamicCast), new[] {assetData}); 

         pinf.SetValue(comp, castedAsset, null); 
        } 
       } 
      } 
     } 

而且here's「DynamicCast」的方法 - 這是我上的博客上發現。這也doesn't工作...

public static class DynamicCast 
{ 
    public static T Cast<T>(object o) 
    { 
     Type ot = o.GetType(); 
     MethodInfo meth = GetMethod(ot, "op_Implicit", typeof(T), 
      BindingFlags.Static | BindingFlags.Public); 
     if (meth == null) 
     { 
      meth = GetMethod(ot, "op_Explicit", typeof(T), 
       BindingFlags.Static | BindingFlags.Public); 
     } 

     if (meth == null) throw new InvalidCastException("Invalid Cast."); 

     return (T) meth.Invoke(null, new[] {o}); 
    } 

    public static MethodInfo GetMethod(Type toSearch, string methodName, 
     Type returnType, BindingFlags bindingFlags) 
    { 
     return Array.Find(
      toSearch.GetMethods(bindingFlags), 
      inf => ((inf.Name == methodName) && (inf.ReturnType == returnType))); 
    } 
} 

的問題是,我必須創建一個AssetData對象(在這種情況下),並將其設置爲屬性值。但是對象很清楚,所以我必須將AssetData的「Asset」屬性投射到「AssetData」。兩者都是相同的類型,但一個是「對象」和一個「T」(Texture2D)。

我該如何施展?

非常感謝!我從中午開始工作......

回答

1

我得到了解決......

我不得不增加一個通用方法「CAST()」到AssetData。我剛來到這個想法,因爲現在我知道我可以調用一個泛型方法;)

Here's解決方案:

if (pinf.PropertyType.IsGenericType) 
{ 
    MethodInfo method = 
     assetData.GetType().GetMethod("Cast").GetGenericMethodDefinition().MakeGenericMethod(
      assetData.AssetType); 

    dynamic castedAsset = method.Invoke(assetData, null); 

    pinf.SetValue(comp, castedAsset, null); 
} 
1

只需使用dynamic即可。

public static class DynamicCast 
{ 
    public static T Cast<T>(object o) 
    { 
     return (T) (dynamic) o; 
    } 
} 

這將自動使用上的o和/或T類型的運行時類型存在的任何隱式/顯式運算符。 (你的代碼是不完整的,因爲你只搜索其中的一個。)

你的問題的其餘部分是非常不清楚的。你需要改寫它。代碼也不清楚:什麼是變量castedAssetType?你只是分配給它,但不使用它。

+0

埃姆,它doesn't工作。但請等待幾分鐘。我正在嘗試一些不同的... – SharpShade 2011-04-09 21:46:57

+1

本來可以很好的提到,這需要C#4.0和DLR – sehe 2011-04-09 22:37:28

+1

@sehe:我還應該提到,這需要一個編譯器嗎?而且它需要一臺電腦?來吧。 – Timwi 2011-04-10 19:10:37

相關問題