I was writing a custom profile provider and had to handle data coming from WCF. Now basically my Profile Provider makes a bridge over WCF to another Profile Provider and I was implementing the GetPropertyValues
. Since a SettingsPropertyCollection
cannot be serialized I created a simple custom class to communicate my desired profile properties:
[DataContract]
public class ProfileKey
{
public ProfileKey()
{
}
public ProfileKey(string name, string type)
{
Name = name;
Type = type;
}
[DataMember]
public string Name { get; set; }
[DataMember]
public string Type { get; set; }
}
So to get the profile values I would send a list of ProfileKeys and a SettingsProperty has 2 important properties to get a profile property from the standard SqlProfileProvider
which was being used behind the WCF service: Name and Type. Having to build the SettingsPropertyCollection myself on the WCF side seemed like no issue like this:
SettingsPropertyCollection properties = new SettingsPropertyCollection();
foreach (ProfileKey key in collection)
{
properties.Add(new SettingsProperty(key.Name) { PropertyType = Type.GetType(key.Type) });
}
To my big surprise the above code did not work! After a lot of experimentation I found that it only works when I use typeof(string)
instead. Flabbergasted by this I ran the following in the immediate window: typeof(string) == Type.GetType("System.String")
and the result was indeed true
?
So my question is what is the difference between typeof(string)
and Type.GetType("System.String")
and if they are indeed equal why doesn't the SqlProfileProvider
return my values when using the later?
This other question may look similar: Type Checking: typeof, GetType, or is?
However, the present question is not about the performance of typeof vs GetType neither does the answer to the other question answer why typeof(string) == GetType("System.String")
results in a TRUE and the SqlProfileProvider will only work for the typeof(string) Type.