NameOf method?
-
I have had to, on a number of occasions, do something like the following:
switch (a.GetType().FullName) {
case "System.Int32":
...
break;
case "MyNamespace.SubNamespace.MyClass":
...
break;
...
}The problem is that if I change class name or the namespace of MyClass, the above breaks without generating a compile-time error. The way that I would like to perform the above is by using the type itself, and have "case typeof(MyClass):", but that DOES generate a compile-time error. I am wondering, then, if there is some method like typeof that gets a constant string representing the class name at compile time, so I could change the above code to...
switch (a.GetType().FullName) {
case nameof(int):
...
break;
case nameof(MyClass):
...
break;
...
}This way, I would get compile-time errors instead of run-time errors if I changed the classname but didn't update the above code. Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
-
I have had to, on a number of occasions, do something like the following:
switch (a.GetType().FullName) {
case "System.Int32":
...
break;
case "MyNamespace.SubNamespace.MyClass":
...
break;
...
}The problem is that if I change class name or the namespace of MyClass, the above breaks without generating a compile-time error. The way that I would like to perform the above is by using the type itself, and have "case typeof(MyClass):", but that DOES generate a compile-time error. I am wondering, then, if there is some method like typeof that gets a constant string representing the class name at compile time, so I could change the above code to...
switch (a.GetType().FullName) {
case nameof(int):
...
break;
case nameof(MyClass):
...
break;
...
}This way, I would get compile-time errors instead of run-time errors if I changed the classname but didn't update the above code. Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
switch
cases take constants so you can't call a function in aswitch
case, so you'll useif
statementsif (a.GetType() == typeof(Int32)) ... else if (a.GetType() == typeof(MyClass)) ...
Eslam Afifi
-
I have had to, on a number of occasions, do something like the following:
switch (a.GetType().FullName) {
case "System.Int32":
...
break;
case "MyNamespace.SubNamespace.MyClass":
...
break;
...
}The problem is that if I change class name or the namespace of MyClass, the above breaks without generating a compile-time error. The way that I would like to perform the above is by using the type itself, and have "case typeof(MyClass):", but that DOES generate a compile-time error. I am wondering, then, if there is some method like typeof that gets a constant string representing the class name at compile time, so I could change the above code to...
switch (a.GetType().FullName) {
case nameof(int):
...
break;
case nameof(MyClass):
...
break;
...
}This way, I would get compile-time errors instead of run-time errors if I changed the classname but didn't update the above code. Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
1. You can use if-else/if statements, instead of a switch 2. Or you may be able to use:
case typeof(MyClass).FullName:
Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com
-
I have had to, on a number of occasions, do something like the following:
switch (a.GetType().FullName) {
case "System.Int32":
...
break;
case "MyNamespace.SubNamespace.MyClass":
...
break;
...
}The problem is that if I change class name or the namespace of MyClass, the above breaks without generating a compile-time error. The way that I would like to perform the above is by using the type itself, and have "case typeof(MyClass):", but that DOES generate a compile-time error. I am wondering, then, if there is some method like typeof that gets a constant string representing the class name at compile time, so I could change the above code to...
switch (a.GetType().FullName) {
case nameof(int):
...
break;
case nameof(MyClass):
...
break;
...
}This way, I would get compile-time errors instead of run-time errors if I changed the classname but didn't update the above code. Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
Once you start relying on the fully qualified name of your class as a string value in another class or application they are pretty much bound to one another.
only two letters away from being an asset
-
I have had to, on a number of occasions, do something like the following:
switch (a.GetType().FullName) {
case "System.Int32":
...
break;
case "MyNamespace.SubNamespace.MyClass":
...
break;
...
}The problem is that if I change class name or the namespace of MyClass, the above breaks without generating a compile-time error. The way that I would like to perform the above is by using the type itself, and have "case typeof(MyClass):", but that DOES generate a compile-time error. I am wondering, then, if there is some method like typeof that gets a constant string representing the class name at compile time, so I could change the above code to...
switch (a.GetType().FullName) {
case nameof(int):
...
break;
case nameof(MyClass):
...
break;
...
}This way, I would get compile-time errors instead of run-time errors if I changed the classname but didn't update the above code. Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
Hi Jeff, you should try not to rely on type names, since doing so will fail for derived types. The way to go most often is using the "is" or "as" keywords, as in:
if (obj is Font) { ... do someting fonty (probably will need a cast) } else if (obj is Panel) { ... deal with a Panel (probably will need a cast) } etc.
or
Font fnt=obj as Font; // generates null if obj isn't a Font if (fnt!=null) { ... do someting fonty to fnt (no extra cast required) } Panel pan=obj as Panel; if (pan!=null) { ... }
The above will also match mySpecialPanel and the like, something typename-dependent code would typically not do. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
switch
cases take constants so you can't call a function in aswitch
case, so you'll useif
statementsif (a.GetType() == typeof(Int32)) ... else if (a.GetType() == typeof(MyClass)) ...
Eslam Afifi
-
I have had to, on a number of occasions, do something like the following:
switch (a.GetType().FullName) {
case "System.Int32":
...
break;
case "MyNamespace.SubNamespace.MyClass":
...
break;
...
}The problem is that if I change class name or the namespace of MyClass, the above breaks without generating a compile-time error. The way that I would like to perform the above is by using the type itself, and have "case typeof(MyClass):", but that DOES generate a compile-time error. I am wondering, then, if there is some method like typeof that gets a constant string representing the class name at compile time, so I could change the above code to...
switch (a.GetType().FullName) {
case nameof(int):
...
break;
case nameof(MyClass):
...
break;
...
}This way, I would get compile-time errors instead of run-time errors if I changed the classname but didn't update the above code. Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
You could also use some lookup pattern.
setup type -> action association
Dictionary<type, somedelegate> myLookup = new .....
myLookup.Add(typeof(string) , () => MessageBox.Show("Its a string!"));
myLookup.Add(typeof(int) , () => Console.WriteLine("ooh an int.."));and then replace your switch/if with: ------
SomeDelegate action;
if (myLookup.TryGetValue( a.GetType() , out action ) )
{
//there is a delegate associated with this type:
action(); //execute the action
}---- So you replace your switch/if completely with the lines above... But it depends on what you actually want to accomplish. See the post above mine regarding subclasses.
-
switch
cases take constants so you can't call a function in aswitch
case, so you'll useif
statementsif (a.GetType() == typeof(Int32)) ... else if (a.GetType() == typeof(MyClass)) ...
Eslam Afifi
I just want to add that this code provide direct type comparison. If you want to compare the inheritance hierarchy use the
is
keyword as Luc Pattyn mentioned here.Eslam Afifi