Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is this TypeConverter not working?
    primarykey
    data
    text
    <p>I am trying to understand why the below code is not working as expected; the <code>TypeDescriptor</code> is simply not picking up the custom converter from the attributes. I can only assume I have made an obvious mistake but I cannot see it.</p> <p>-- edit -- this code seems to work when I run it in a console on its own, I'm actually calling a converter from within a much more complex application and from a different namespace.</p> <p>-- edit -- alternatively any suggestions on how I can debug the TypeDescriptor so I can see what is going on and then I can probably answer this myself.</p> <p>-- edit -- this problem is almost certainly related to pieces being in different assemblies.</p> <p>-- edit -- It looks like this is not working because of some quirk of loading assemblies dynamically - this code is running under a plugin like architecture.</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.ComponentModel; namespace MyTest { public class TestTester { public static void Main(string[] args) { object v = TypeDescriptor.GetConverter(typeof(MyTest.Test)).ConvertFromInvariantString("Test"); } } public class TestConverter : TypeConverter { public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return false; } public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType) { if (sourceType == typeof(string) || base.CanConvertFrom(context, sourceType)) { return true; } return base.CanConvertFrom(context, sourceType); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(Test) || base.CanConvertTo(destinationType)) { return true; } return base.CanConvertTo(context, destinationType); } public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if (value.GetType() == typeof(string)) { Test t = new Test(); t.TestMember = value as string; return t; } return base.ConvertFrom(context, culture, value); } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string) &amp;&amp; value.GetType() == typeof(Test)) { return ((Test)value).TestMember; } return base.ConvertTo(context, culture, value, destinationType); } } [TypeConverterAttribute(typeof(TestConverter))] public struct Test { public string TestMember { get; set; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload