所以我在XNA,C#4.0中创建一个游戏,我需要管理很多PowerUps(在代码中都继承自"PowerUp"类),并处理PowerUps的后端管理我目前有一个枚举,PowerupEffectType,具有PowerUp的每个子类的值.最终在代码中我需要进行从PowerupEffectType到Powerup类型的转换(Type通常用类实现typeof([class name])).
由于这是一个组项目,我想尽可能将PowerupEffectType的每个值与其对应的类Type结合,即:不仅仅是期望我的其他程序员使用switch语句手动进行转换,并确保以后添加/扩展在尽可能少的地方进行尽可能少的变更.我有几个选项,到目前为止我发现的最好的是创建枚举伪方法,将所有内容压缩到一个switch语句(我想要的99%),这要归功于我在这里找到的一些提示:http ://msdn.microsoft.com/en-us/library/bb383974.aspx
但我试图更进一步 - 我可以保存Type一个enum吗?我知道你可以将枚举保存为特定类型(链接:http://msdn.microsoft.com/en-us/library/cc138362.aspx),但Type不是其中之一.当前选择是byte,sbyte,short,ushort,int,uint,long和ulong.是否有任何可行的方法将转换a保存Type到上述任何数据类型并返回?
为了清楚起见,这就是我希望我能做到的,我正在寻找一种方法:
// (Assuming 'LightningPowerup', 'FirePowerup', and 'WaterPowerup' are
// all declared classes that inherit from a single base class)
public enum PowerupEffectType
{
LIGHTNING = typeof(LightningPowerup),
FIRE = typeof(FirePowerup),
WATER = typeof(WaterPowerup)
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点,或者我只是过度复杂解决了已经99%完成的问题?
提前致谢!
在C#中是否有任何方法可以保存函数调用以便以后调用?例如,我希望能够说:
public class MyFunctionCaller
{
public static void SaveFunctionCallForLater(/* Some parameters*/)
{
// Save the Function Call in a variable or a list to be invoked after a set amount of time
}
}
public class MyProgram
{
public static void Main()
{
// Save a call to 'SpecialFunction' along with its parameters,
// and maybe the object that's calling it (not applicable in
// this example context, but important to my question)
MyFunctionCaller.SaveFunctionCallForLater(/* Stuff */);
}
public void SpecialFunction(/* …Run Code Online (Sandbox Code Playgroud) 所以我试图从我的C#.NET应用程序调用Oracle存储过程.我可以找到的大多数在线参考建议"使用System.Data.OracleClient;",但.Net 3.5无法识别该命名空间,因此我使用的是"Oracle.DataAccess.Client".
下面是我的代码的一些解释,以前设置和测试的OracleConnection称为'myConn'已经填充了参数':arg_myArg'(如果重要的话,这是一个数字):
command.Connection = myConn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "exec mySchema.myProc(:arg_myArg)"
command.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)
诀窍在于程序没有按设计返回,它只是填充了我从中拉出的不同表.但是,当我尝试运行上面的代码时,我在最后一行得到一个'OracleException'并给出了这个错误:
ORA-06550: line 1, column 13:
PLS-00103: Encountered the symbol "MYSCHEMA" when expecting one of the following:
:= . ( @ % ;
The symbol ":=" was substituted for "MYSCHEMA" to continue.
Run Code Online (Sandbox Code Playgroud)
从命令中删除"exec"会出现此错误:
ORA-06550: line 1, column 8:
PLS-00801: internal error [22503]
ORA-06550: line 1, column 8:
PL/SQL: Statement ignored
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?我很乐意澄清任何事情
这是我第一次在stackoverflow.com和我上周的这份工作上发帖,所以我很感激你的理解和相对急于搞清楚这一点
这是我第一次在Stack Overflow上发帖,所以希望我做的一切正常,你们可以提供帮助.
我想知道在C#中是否有一种方法来访问属于类的静态变量,只给出类的类型.例如:
public class Foo
{
public static int bar = 0;
}
public class Main
{
public void myFunc(Type givenType)
{
int tempInt = ??? // Get the value of the variable "bar" from "Foo"
Debug.WriteLine("Bar is currently :" + tempInt);
}
}
// I didn't run this code through a compiler, but its simple enough
// that hopefully you should get the idea...
Run Code Online (Sandbox Code Playgroud)
很难描述需要知道这一点的背景,但我正在XNA中制作游戏,我正在尝试使用引用计数来降低设计的复杂性.我在游戏和强化中有对象可以应用它们(留在对象上).上电可能会死亡,但它们的影响仍然可以在物体上停留,我需要跟踪上电中是否有任何影响仍在物体上停留(因此,引用计数).我打算用一个静态整数制作一个"PowerUpEffect"类(对于每种类型的加电),保存仍然受其影响的对象数量,但是传递PowerUpEffect时游戏其余部分的设计效果不佳一直到对象,它调用PowerUpEffect类的方法.
我希望只传递PowerUpEffect的类型(使用类似"typeOf()"的东西)并使用该类型来引用属于这些类型的静态变量,但我不知道如何做或者甚至可能.
我很高兴甚至找到不能直接回答这些问题的解决方案,而是通过简单而优雅的设计解决问题.=)
救命!(谢谢!)