0

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.

Community
  • 1
  • 1
IvanL
  • 2,475
  • 1
  • 26
  • 39
  • See this answer: http://stackoverflow.com/a/17495443/745969 – Tim Nov 07 '14 at 09:32
  • Your test showed that the **results** were equal, not the functions themselves. That comparison should return false. – ArtOfCode Nov 07 '14 at 09:40
  • Just to make sure, `key.Type` is really `"System.String"` for all keys? –  Nov 12 '14 at 09:31
  • Yes all keys are System.String (for now) that's why as temporary workaround I have placed typeof(string) to have code work. But in the near future we're planning on adding different profile properties that are not string. – IvanL Nov 12 '14 at 12:29

0 Answers0