I recently came across this (highly-voted) community wiki, which states
you can't use
string
in reflection; you must useString
.
however, since the answer is from 2011 and since I have never experienced any issues with using string
in reflection scenarios, I was wondering whether the statement still holds true.
Additionally, taking into account that both string
and System.String
get compiled to System.String
and string
being merely an alias for System.String
, I don't think there are any potential differences, let alone problems.
I tried to find differences or problems when using string
in reflection, but I couldn't find any:
Getting the Type:
string a = "Foo";
System.String b = "Foo";
Type aType = a.GetType(); // yields System.String - big surprise ;)
Type bType = a.GetType(); // yields System.String
Calling a method at runtime:
string a = "Foo Bar";
System.String b = "Foo Bar";
bool aContainsBar = (bool) a.GetType()
.GetMethod("Contains")
.Invoke(a, new object[] { "Bar" });
// yields true - big surprise again ;)
bool bContainsBar = (bool) b.GetType()
.GetMethod("Contains")
.Invoke(a, new object[] { "Bar" });
// also yields true
200+ votes don't lie - I am very interested in what differences between using string
and System.String
in reflection there are / were.
Is the statement still correct? If so, can you please provide me with an example showing the difference between string
and System.String
?