How to approach refactoring this code (C #, Types)

Firstly, my apologies for the title of the question. I'm not quite sure what he called what I'm trying to accomplish, so I just go to him.

I am currently developing a game engine in C # using GDI + and have implemented a component class. The idea is that any game object can have several components to it (like Unity3D), and I want to find any component of any type by looking for what class it is.

In this note, I would like to modify this piece of code:

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

To look like this:

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

How should I do it?

Sorry if my question seems vague, any light on this topic will be wonderful!

+4
8

. , :

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

, List<Component> obj. 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>();

+3

, :

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

}
+1

GetComponentOfType, ( , ), . Rigidbody < > .

.

: , .

, .

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; }
    }
Hide result

Rigidbody. , . Olso , , .

, : var r = obj.GetComponentOfType(); datat var. var , - oldscool ( # 1 C2.0). . , var ( , )

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));
    }
}

, , .

0

Source: https://habr.com/ru/post/1671614/


All Articles