2017-03-06 26 views
4

首先,我對問題的標題表示歉意。我不確定它是什麼,我正在努力完成,所以我會直接去解決它。如何處理重構此代碼(C#,Types)

我目前正在使用GDI +在c#中開發一個遊戲引擎,並且已經實現了一個組件類。這個想法是任何遊戲對象都可以有多個組件(與Unity3D一樣),我希望能夠通過搜索它是什麼類來找到任何類型的組件。

關於這一點,我想改變這一段代碼:

Rigidbody r = obj.GetComponentOfType(typeof(Rigidbody)) as Rigidbody; 

看起來像這個:

Rigidbody r = obj.GetComponentOfType<Rigidbody>(); 

我如何去這樣做呢?

對不起,如果我的問題似乎含糊不清,這個主題的任何燈光將是美好的!

+1

http://answers.unity3d.com/questions/55331/differences-generic-getcomponentscript-vs-getcompo.html – lordkain

回答

0

向GetComponentOfType方法添加一個擴展方法,該方法爲您做了髒工作(您發佈的第一行代碼),並讓它接受泛型類型。然後您可以傳入<>標籤中的Rigidbody類型。

欲瞭解更多關於擴展方法的信息去here

編輯:確保您的擴展方法是靜態的。

希望它有幫助。

3

假設您的obj類型中有一個List<Component>字段。您可以使用Linq

// assuming 
public class GameObject 
{ 
    List<Component> m_Components; // all components attached to this GameObject 
    public TComponent GetComponentOfType<TComponent>() 
     where TComponent : Component 
    { 
     // Select only the ones of type TComponent and return first one 
     return m_Components.OfType<TComponent>().FirstOrDefault(); 
    } 
} 

要使用此:obj.GetComponentOfType<Rigidbody>();

1

如果你不能改變類,你可以使用擴展方法:

public static T GetComponentOfType<T>(this [objType] obj) 
where T : class 
{ 
    return obj.GetComponentOfType(typeof(T)) as T; 
} 
7

這就是所謂的通用方法。您可以創建一個過載來調用您現有的基於類型的實現,如下所示:

public T GetComponentOfType<T>() 
{ 
    return (T) GetComponentOfType(typeof(T)); 
} 
0

這是通過使用泛型完成的。 GetComponentofType方法的主體幾乎是相同的,唯一的區別是你通過泛型指定了你想要的類型,而不是將它作爲參數傳入。

的方法可能如下:

public T GetComponentOfType<T>(){ 

     //code goes here to locate the object. 
     you can get the type via typeof(T) 

     return theObject; 
} 
0

public class Rigidbody{ 
 
     public int Size{ get; set; } 
 
     public string fourgroundColor{ get; set; } 
 
     public string backgroundColor{ get; set; } 
 
     public int fingerammount{ get; set; } 
 
     public int arms{ get; set; } 
 
    }

創建一個名爲剛體類。將所有組件設置爲類,比你可以使用它。 Olso使它公開,否則你不能像你的例子那樣使用它。當你有了類的時候你可以說:var r = obj.GetComponentOfType(); 還使用var而不是數據類型。 var是更好的數據類型是oldscool(更像c#1或C2。0)我每次都犯同樣的錯誤。但是當我開始爲軟件開發商,工作,他們告訴我使用VAR(我還只是一個初級,olmost medior)

+0

IMO這個答案根本沒有意義。 OP詢問了如何創建一個通用的方法來提取他的組件,並且您正在編寫關於編譯時類型解析的方法。 –

1

我想你想實現像下面這樣的通用方法:

using System.Linq; 
using System.Collections.Generic; 

/// <summary> 
/// Base-Class 
/// </summary> 
public abstract class Thing 
{ 
    private ICollection<Thing> _things; 

    public Thing() 
    { 
     _things = new List<Thing>(); 
    } 

    public void AddSomething(Thing toAdd) 
    { 
     _things.Add(toAdd); 
    } 

    /// <summary> 
    /// Assuming that every type can only appear once 
    /// </summary> 
    /// <param name="t"></param> 
    /// <returns></returns> 
    public T GetComonentOfType<T>() where T : Thing 
    { 
     return this.GetComonentOfType(typeof(T)) as T; 
    } 

    public Thing GetComonentOfType(Type t) 
    { 
     return _things.Where(x => x.GetType() == t).Single(); 
    } 

} 

/// <summary> 
/// One possible implementation 
/// </summary> 
public class SpecialThing : Thing 
{ 

} 
0

假設所有組件類繼承這個類(或類似):

public abstract class IComponent 
{ 
    protected IList<object> objComponents; 

    public object GetComponentOfType(Type type) 
    { 
     if (objComponents == null) 
      return null; 

     return objComponents.Where(obj => obj.GetType() == type).FirstOrDefault(); 
    } 
} 

你可以只創建另一個主要的媽媽級獲得來自繼承:

public abstract class BaseComponent : IComponent 
{ 
    public object GetComponentOfType<T>() 
    { 
     return GetComponentOfType(typeof(T)); 
    } 
} 

或者如果您有權訪問第一類,只需將前一個函數添加到它。