【问题标题】:variable object in c#c#中的变量对象
【发布时间】:2012-08-26 03:19:22
【问题描述】:

我有一个名为 gameObject 的类,其中一个属性名为 component,类型为 object

public object component;

我正在尝试使用这个object 作为一个对象,它可以保存你给它的任何类的对象。例如

unit c = new unit(...)
gameObject test = new gameObject(...)
test.component = c;

我想通过组件对象使用 c 对象。例如

if(test.component==typeof(unit))
    test.component.Draw();//draw is a function of the unit object

这可能吗?我该怎么做?

【问题讨论】:

  • 您可以指定为组件的类型范围有哪些?你能想到他们应该有的一组共同行为吗?
  • 如果您使用泛型,您可以将 component 设为强类型属性。

标签: c# variables object typeof


【解决方案1】:

typeof(unit)也是一个对象,和component不同。你应该改用component.GetType()

if(test.component.GetType()==typeof(unit))

这不会检查派生类型,但会:

if(test.component is unit)

不过,这并不能让你在没有强制转换的情况下调用Draw。最简单的同时检查和施法的方法如下:

unit u = test.component as unit;
if (u != null) {
    u.Draw();
}

【讨论】:

    【解决方案2】:

    是的,它被称为铸造。像这样:

    if(test.component is unit)
      ((unit)test.component).Draw();
    

    【讨论】:

      【解决方案3】:

      使用接口并使单元类实现该接口。在接口中定义Draw()方法,并将组件声明为你定义的接口的类型。

      public interface IDrawable
      {
       void Draw();
      }
      
      public IDrawable component;
      
      public class unit : IDrawable
      ...
      

      【讨论】:

        【解决方案4】:

        if (test.component is unit) ((unit)(test.component)).Draw();

        【讨论】:

          【解决方案5】:

          正如其他人所说-您可以将所有内容投射到Object。但是你应该使用接口的一些基类来确保没有人会向你发送Int32 或其他一些意想不到的类型。 但是如果所有支持的类型都有一些通用的方法,那么最好把它放在接口中。所以你可以Draw() 任何东西而不用强制转换,甚至不知道它到底是什么。

          具有空接口的变体只是强制执行正确的类型:

          interface IComponent
          {
          }
          class unit : IComponent
          {
              //...
          }
          class enemy : IComponent
          {
              //...
          }
          class gameObject
          {
              IComponent component;//now only objects inheriting IComponent can be assigned here
          
              public void DrawComponent()
              {
                  unit u = component as unit;
                  if (u != null) {
                      u.Draw();
                  }
                  enemy e = component as enemy;
                  if (e != null) {
                      e.DrawIfVisible();
                  }
              }
          }
          

          接口声明通用功能的变体:

          interface IDrawable
          {
              void Draw();
          }
          class unit : IDrawable
          {
              public void Draw(){;}
              //...
          }
          class enemy : IDrawable
          {
              public void Draw(){;}
              //...
          }
          
          
          class gameObject
          {
              IDrawable component;
          
              public void DrawComponent()
              {
                  //now you don't need to care what type component really is
                  //it has to be IDrawable to be assigned to test
                  //and because it is IDrawable, it has to have Draw() method
                  if (component != null) {
                      component.Draw();
                  }
              }
          }
          

          【讨论】:

            【解决方案6】:

            要使用对象的特定行为,您必须将其强制转换为特定类型。 使用 object 变量引用它,您只能将其用作 object 实例。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-03-11
              • 1970-01-01
              • 2021-07-11
              • 2021-09-12
              • 1970-01-01
              • 2011-12-08
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多