3

I made an example of UserControl and still (spent whole day) unable to fix my problem.

I want UserControl to have a complex property, to see that property inside property grid at design-time, able to change it so on. Complex property is simple - its a class with one string property.

Now i have 2 problems:

1) When i change the value of text at property grid - the value dont goes to Form1.Designer.cs

2) Sometimes when i rebuild project (even dont need to run) i have a VS's popup where said SettingsCoverter is unable to convert Settings to InstanceDescriptor. Those Settings classes are mine. Please help to fix that.

 [TypeConverter(typeof(SettingsConverter))]
public class Settings : INotifyPropertyChanged
{
    private string stringText = "123";
    public string StringText
    {
        get { return stringText; }
        set
        {
            stringText = value;
            OnPropertyChanged("StringText");
        }
    }

    public Settings()
    {
    }

    public Settings(string fText)
    {
        StringText = fText;
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(name));
    }

    #region Implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

and

class SettingsConverter : ExpandableObjectConverter
{
    public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
    {
        return true;
    }

    public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
    {
        return new Settings((string)propertyValues["StringText"]);
    }

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string))
            return true;
        return base.CanConvertFrom(context, sourceType);
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(string) || destinationType == typeof(InstanceDescriptor))
            return true;
        return base.CanConvertTo(context, destinationType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (value is Settings)
        {
            if (destinationType == typeof(InstanceDescriptor))
            {
                Settings settings = (Settings)value;
                object[] properties = new object[1];
                Type[] types = new Type[1];

                types[0] = typeof(string);
                properties[0] = settings.StringText;

                ConstructorInfo ci = typeof(Settings).GetConstructor(types);
                return new InstanceDescriptor(ci, properties);
            }

            if (destinationType == typeof(string))
            {
                Settings settings = (Settings)value;
                return settings.StringText;
            }
        }

        return base.ConvertTo(context, culture, value, destinationType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        if (value == null)
            return "";

        if (value is string)
            return new Settings(value as string);

        return base.ConvertFrom(context, culture, value);
    }
}
Alexander
  • 431
  • 2
  • 5
  • 19

1 Answers1

0

2) here is the advise

http://forums.asp.net/t/1309871.aspx?TypeConverter+Error+InstanceDescriptor+

The type converters go straight to the CLR's cache of type information and do not go through the type resolution service implemented by the project systems. This means that the CLR will return the type from the assembly it had previously loaded prior to the rebuild - and the conversion will fail since the type on the design surface is being loaded from the newly built assembly. I verified that you can work around this by ensuring the version number of the class library project gets auto-incremented on each build. You do this by the following: 1) Bring up properties on the class library project 2) Select the "Application" tab and click the "Assembly Information..." button. 3) In version the field set the last entry to a "*" - so it should say: 1 0 0 *

Now whenever the classlibrary is built, the revision number (the last digit of the version) will be auto-incremented. This forces the CLR to invalidate the entry it has cached and load the new one.

oudi
  • 577
  • 4
  • 11