C# 在XNA游戏中使用泛型访问类
如果我有一个基于另一个类的类,如果第一个类可以有任何名称,我如何访问它的属性?我想用泛型来访问属性,但泛型之所以“泛型”是有原因的 例如:C# 在XNA游戏中使用泛型访问类,c#,generics,reference,xna,C#,Generics,Reference,Xna,如果我有一个基于另一个类的类,如果第一个类可以有任何名称,我如何访问它的属性?我想用泛型来访问属性,但泛型之所以“泛型”是有原因的 例如: public class AGameInXNA : Microsoft.Xna.Framework.Game { int ExampleGameProperty; } // ... another class ... // public class ReferenceToAGameInXNA { Game gameInstance;
public class AGameInXNA : Microsoft.Xna.Framework.Game
{
int ExampleGameProperty;
}
// ... another class ... //
public class ReferenceToAGameInXNA
{
Game gameInstance;
public void SetGameInstance(Game game)
{
gameInstance = game;
}
public void SetExampleGameProperty()
{
gameInstance.ExampleGameProperty = 21; // I don't know the name of
// AGameInXNA, so I want to
// access it using a generic
// class.
}
}
我知道这不起作用,所以如果我不知道AGameInXNA的名称,那么在本例中如何使用泛型来访问另一个类中的AGameInXNA
属性
编辑:我正在尝试这样做,以便以后可以重用此代码。我希望能够拥有一个未知的类,例如扩展另一个类的
Microsoft.Xna.Framework.Game
,例如public class unknownclassname
,并且能够访问类unknownclassname
,而无需在库代码中直接调用/实现它。我不认为泛型是您真正想要的。在第二个类中,只需将所有gameInstance
的类型更改为您为游戏创建的类的类型,在本例中为AGameInXNA
。在每个XNA游戏中应该只需要游戏类型的一个子类。这将允许您从引用类访问AGameInXNA
的任何公共成员
如果这不是您想要的,请详细解释您想要完成的任务,我会尽力帮助您。我不知道XNA,但是如果您希望有几个类继承自
游戏
,并且在所有类上具有相同的属性,您可以创建一个从Game
继承的抽象类,并让其他类从该类继承
(另外,您的GetGameInstance()
名称不正确,因为它设置了字段,但没有得到它。而且作为属性可能更好。)
如果具有
ExampleGameProperty
的其他类不应从Game
继承,则可以创建一个接口AGameInXNA
将直接从Game
继承,它还将实现接口。您可以在ReferenceToAGameInXNA
中使用该界面,我建议您查看一下
例如,您可以创建一个服务,它可以像
interface IExamplePropertyService
{
int ExampleProperty { get; set; }
}
public class AGameInXNA : Microsoft.Xna.Framework.Game, IExamplePropertyService
{
int ExampleGameProperty { get; set; }
void Initialize()
{
// Do other initialization
Services.Add( typeof(IExamplePropertyService), this );
}
}
public class ReferenceToAGameInXNA
{
IExamplePropertyService propertyService;
public void GetGameInstance(Game game)
{
propertyService = (IExamplePropertyService)game.GetService( typeof(IExamplePropertyService) );
}
public void SetExampleGameProperty()
{
propertyService.ExampleGameProperty = 21;
}
}
实现它,并在游戏组件中注册它,然后在您的ReferenceToAGameInXNA中,您将查询此服务并存储它(而不是游戏)以供以后使用
作为一个额外的好处,IExamplePropertyService甚至不再需要由游戏类实现,它可以由任何游戏组件实现
这使得我们可以很容易地将类与游戏中其他类的内部工作机制分离开来。只要服务存在于某个地方,就可以使用您的ReferenceToGameinXNA。使用“Game gameInstance”;您无法访问ExampleProp。您也应该使用“AGameInXNA gameInstance;”访问ExampleProp。如果我有一些仅在
AGameInXNA
中定义的属性,我将如何访问这些特定属性?那么您就知道它是AGameInXNA
,您应该这样对待它。如果你得到的是游戏
,但你知道它实际上是AGameInXNA
,你可以使用铸造。
interface IExamplePropertyService
{
int ExampleProperty { get; set; }
}
public class AGameInXNA : Microsoft.Xna.Framework.Game, IExamplePropertyService
{
int ExampleGameProperty { get; set; }
void Initialize()
{
// Do other initialization
Services.Add( typeof(IExamplePropertyService), this );
}
}
public class ReferenceToAGameInXNA
{
IExamplePropertyService propertyService;
public void GetGameInstance(Game game)
{
propertyService = (IExamplePropertyService)game.GetService( typeof(IExamplePropertyService) );
}
public void SetExampleGameProperty()
{
propertyService.ExampleGameProperty = 21;
}
}