【发布时间】:2018-12-01 10:19:19
【问题描述】:
有没有办法在函数中检查参数是否为静态变量?
这将帮助我防止任何拼写错误的可能性,其中用户尝试快速设置单例,但在通过 @ 提供给我们之前忘记将自己的 instance 成员声明为 static 987654324@
这是我的功能:
// returns 'false' if failed, and the object will be destroyed.
// Make sure to return from your own init-function if 'false'.
public static bool TrySet_SingletonInstance<T>(this T c, ref T instance) where T : Component {
//if(instance is not static){ return false } //<--hoping for something like this
if(instance != null){
/* cleanup then return */
return false;
}
instance = c;
return true;
}
【问题讨论】:
-
好吧,如果不可能,- 一种方法是使用不同的参数名称。例如
ref T instance_VERIFY_IS_STATIC。是的,丑陋,但会让任何试图使用我们功能的人清醒 -
我觉得你可以使用反射来获取关于T的信息见:stackoverflow.com/questions/196936/reflection-and-generic-types