It's "not working" as as
object.GetType()
does not return string; the return type is
System.Type
. It can work, but only if you need to use reflection seriously: for example check if the type supports certain interface, invoke type constructor to obtain instance and work with the instance using techniques which are
agnostic
to the type.
In more typical situation you just may need to know is the instance of the certain type (in case of class your can check if the instance is of the type belonging to some inheritance hierarchy) or perform
dynamic type cast
to conditionally down-case more abstract compile type to some more concrete run-time type. For these purposes, you need to use operators "
is
" and "
as
":
object @object;
@object = new
if (@object is string) {
string @string = (string)@object);
}
string @string = @object as string;
if (@string != null)
Note: "
@object
" and "
@string
" in C# syntax are normal valid variable identifiers, "@" is just the way to create identifiers identical to language keywords.
—SA