5

तो मैं इस तरह एक विन्यास है कि एक ConfigurationSection/ConfigurationElementCollection मिल गया है:मैं कॉन्फ़िगरेशनसेक्शन के साथ TypeConverters का उपयोग कैसे कर सकता हूं?

public class MimeFormatElement: ConfigurationElement 
{ 
    #region Constructors 
    /// <summary> 
    /// Predefines the valid properties and prepares 
    /// the property collection. 
    /// </summary> 
    static MimeFormatElement() 
    { 
     // Predefine properties here 
     _mimeFormat = new ConfigurationProperty(
      "mimeFormat", 
      typeof(MimeFormat), 
      "*/*", 
      ConfigurationPropertyOptions.IsRequired 
     ); 
    } 
    private static ConfigurationProperty _mimeFormat; 
    private static ConfigurationPropertyCollection _properties; 

    [ConfigurationProperty("mimeFormat", IsRequired = true)] 
    public MimeFormat MimeFormat 
    { 
     get { return (MimeFormat)base[_mimeFormat]; } 
    } 
} 

public class MimeFormat 
{ 
    public string Format 
    { 
     get 
     { 
      return Type + "/" + SubType; 
     } 
    } 
    public string Type; 
    public string SubType; 

    public MimeFormat(string mimeFormatStr) 
    { 
     var parts = mimeFormatStr.Split('/'); 
     if (parts.Length != 2) 
     { 
      throw new Exception("Invalid MimeFormat"); 
     } 

     Type = parts[0]; 
     SubType = parts[1]; 
    } 
} 

और स्पष्ट रूप से मैं एक TypeConverter है कि वास्तव में करता है की जरूरत है:

<mimeFormats> 
    <add mimeFormat="text/html" /> 
</mimeFormats> 

और यहाँ है कि कैसे मैं mimeFormats संभाल कुछ (इस खाली खोल के बजाय):

public class MimeFormatConverter : TypeConverter 
{ 
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
    { 
     throw new NotImplementedException(); 
    } 
    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) 
    { 
     throw new NotImplementedException(); 
    } 

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
    { 
     throw new NotImplementedException(); 
    } 
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
    { 
     throw new NotImplementedException(); 
    } 
} 

कैसे मैं एक TypeConverter उस प्रकार conve की अनुमति देगा सेट कर सकता हूं स्ट्रिंग से/स्ट्रिंग तक? मैंने एमएसडीएन उदाहरणों का उपयोग करने की कोशिश की है लेकिन मुझे त्रुटि संदेश मिल रहा है:

टाइप कनवर्टर System.String से परिवर्तित नहीं हो सकता है।

अनिवार्य रूप से, यह कैसे स्थापित किया जा सकता है ताकि यह केवल कॉन्फ़िगरेशनसेक्शन जो भी करने का प्रयास कर रहा हो, उसके साथ काम करेगा?

उत्तर

1

मैंने इसे समझ लिया।

public class MimeFormatElement: ConfigurationElement 
{ 
    #region Constructors 
    /// <summary> 
    /// Predefines the valid properties and prepares 
    /// the property collection. 
    /// </summary> 
    static MimeFormatElement() 
    { 
     // Predefine properties here 
     _mimeFormat = new ConfigurationProperty(
      "mimeFormat", 
      typeof(MimeFormat), 
      "*/*", 
      ConfigurationPropertyOptions.IsRequired 
     ); 

     _properties = new ConfigurationPropertyCollection { 
      _mimeFormat, _enabled 
     }; 
    } 
    private static ConfigurationProperty _mimeFormat; 
    private static ConfigurationPropertyCollection _properties; 

    [ConfigurationProperty("mimeFormat", IsRequired = true)] 
    public MimeFormat MimeFormat 
    { 
     get { return (MimeFormat)base[_mimeFormat]; } 
    } 
} 

/*******************************************/ 
[TypeConverter(typeof(MimeFormatConverter))] 
/*******************************************/ 
public class MimeFormat 
{ 
    public string Format 
    { 
     get 
     { 
      return Type + "/" + SubType; 
     } 
    } 
    public string Type; 
    public string SubType; 

    public MimeFormat(string mimeFormatStr) 
    { 
     var parts = mimeFormatStr.Split('/'); 
     if (parts.Length != 2) 
     { 
      throw new Exception("Invalid MimeFormat"); 
     } 

     Type = parts[0]; 
     SubType = parts[1]; 
    } 
} 

public class MimeFormatConverter : TypeConverter 
{ 
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
    { 
     return sourceType == typeof(string); 
    } 
    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) 
    { 
     return new MimeFormat((string)value); 
    } 

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
    { 
     return destinationType == typeof(string); 
    } 
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
    { 
     var val = (MimeFormat)value; 
     return val.Type + "/" + val.SubType; 
    } 
} 
+1

आपको मैन्युअल रूप से 'टाइप कनवर्टर' प्राप्त करने की आवश्यकता नहीं है। 'TypeDescriptor.GetConverter ... हटाएं। जैसा आपने किया था, संपत्ति के लिए 'टाइप कनवर्टर' को आश्वस्त करना या स्वयं टाइप करना पर्याप्त है। –

0

इस बिंदु से, आप ConvertTo भीतर परिवर्तित वर्गों और ConvertFrom तरीकों

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

    try { 
    if (value is string) { 
     string s = (string)value; 

     // here is where you look at the string to figure out the MimeFormat 
     // like so.... 
     return new MimeFormat(s); 
    } 


    throw new NotSupportedException(NotSupportedException(value.GetType(), typeof(MimeFormat)); 
} 

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { 
    if (value == null) 
    return null; 

    MimeFormat p = (MimeFormat)value; 
    if (destinationType == typeof(String)) 
    return p.ToString(); 

    throw new NotSupportedException(NotSupportedException(typeof(MimeFormat), destinationType)); 
} 

तुम भी रूप में अच्छी तरह CanConvert कार्यों ओवरराइड करने के लिए की जरूरत है संपादित बनाना होगा।

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

public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { 
    if (destinationType == typeof(string)) 
    return true; 
    return false; 
} 
+0

काम नहीं करता है। मुझे अभी भी त्रुटि संदेश मिलता है: संपत्ति 'mimeFormat' का मान पार्स नहीं किया जा सकता है। त्रुटि यह है: TypeConverter System.String से कनवर्ट नहीं कर सकता है। –

2

इस प्रयास करें:

TestSection.cs

public class TestSection : ConfigurationSection 
{ 

    private static readonly ConfigurationProperty sFooProperty = new ConfigurationProperty("Foo", 
                          typeof(Foo), 
                          null, 
                          new FooTypeConverter(), 
                          null, 
                          ConfigurationPropertyOptions.None); 

    public static readonly ConfigurationPropertyCollection sProperties = new ConfigurationPropertyCollection(); 

    static TestSection() 
    { 
     sProperties.Add(sFooProperty); 
    } 

    public Foo Foo 
    { 
     get { return (Foo)this[sFooProperty]; } 
     set { this[sFooProperty] = value; } 
    } 

    protected override ConfigurationPropertyCollection Properties 
    { 
     get { return sProperties; } 
    } 

} 

Foo.cs

public class Foo 
{ 

    public string First { get; set; } 
    public string Second { get; set; } 

    public override string ToString() 
    { 
     return First + ',' + Second; 
    } 

} 

FooTypeConverter.cs

public class FooTypeConverter : TypeConverter 
{ 

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

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) 
    { 
     string val = value as string; 

     if (val != null) 
     { 
      string[] parts = val.Split(','); 

      if (parts.Length != 2) 
      { 
       // Throw an exception 
      } 

      return new Foo { First = parts[0], Second = parts[1] }; 
     } 

     return null; 
    } 

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
    { 
     return (destinationType == typeof(string)); 
    } 

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
    { 
     Foo val = value as Foo; 

     if (val != null) 
      return val.ToString(); 

     return null; 
    } 

} 
+0

काम नहीं करता है। मुझे अभी भी त्रुटि संदेश मिलता है: संपत्ति 'mimeFormat' का मान पार्स नहीं किया जा सकता है। त्रुटि यह है: TypeConverter System.String से कनवर्ट नहीं कर सकता है। –

+0

@ डेविड मर्डोक, मैंने अपडेट किया है। उपरोक्त कोड मेरे लिए काम किया। –

+0

मुझे एहसास हुआ कि मैंने सूचना का एक महत्वपूर्ण टुकड़ा छोड़ा है; मैं वास्तव में कॉन्फ़िगरेशन एलिमेंट के कन्स्ट्रक्टर में कॉन्फ़िगरेशनप्रॉपर्टी सेट कर रहा हूं। मैं इसे प्रतिबिंबित करने के लिए अपने प्रश्न को संपादित कर रहा हूं। –

1

आप संपत्ति पर TypeConverterAttribute डाल serializer यह कैसे संभाल करने के लिए बताने के लिए कर सकते हैं: यहाँ समाधान है।

[TypeConverter(typeof(MimeFormatConverter))] 
[ConfigurationProperty("mimeFormat", IsRequired = true)] 
public MimeFormat MimeFormat 
{ 
    get { return (MimeFormat)base[_mimeFormat]; } 
} 
संबंधित मुद्दे