2017-03-06 70 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)); 
    } 
} 

或者如果您有权访问第一类,只需将前一个函数添加到它。