- Home /
How to make a "Type" variable?
I've been trying to make a little static functions, and I require to use a Type variable, however, I can't find a way to make one. System.Type does not work, and simply typing in Type will get me an error. Here's one example of a function I'd like to do.
static function IsType(obj:Object,t:Type){ //will throw an error
var bool = false;
if(typeof(obj).IsSubclassOf(typeof(type)) || obj.GetType() == type){
bool = true;
}
return bool;
}
"Assets/stuff/Scripts/Interface_Scripts/Global.js(13,40): BCE0018: The name 'Type' does not denote a valid type ('not found'). Did you mean 'System.Type'?"
I have other situations where I need a type variable, and it would be a lot of help if there's a solution for this. Thanks.
I'd guess you actually don't need a type variable there you could just use object - however, you're best bet is to actually use IsAssignableFrom which stops the need for the extra test and also works for interfaces...
static function IsType(obj: object, type: object) {
return type.IsAssignableFrom(obj.GetType());
}
Answer by RedDevil · Feb 14, 2014 at 02:31 PM
Check out this answer: http://answers.unity3d.com/questions/212177/issue-casting-using-type-variable-c.html
Your answer
Follow this Question
Related Questions
Codes unavailable in C# 0 Answers
Cannot cast from source type to destination type? C# 0 Answers
C# Generic return type 1 Answer
Not Rotating Properly 2 Answers
Return array of transform 2 Answers