0

I've researched C# compiler error CS1061 but I'm not sure how to resolve the error generated by this code. I am trying to design a general purpose TypeConverter class that uses a generic type.

First, I define a class to be used as an expandable property. It has a static method 'ConvertFromString' which the TypeConverter will call.

[TypeConverter(typeof(MyClassConverter))]
public class MyClass
{
  ...
  public static MyClass ConvertFromString(string s, MyClass OldValue)
  {
  ...
  }
}

I'd like to define a TypeConverter like this,

public class MyClassConverter<T> : ExpandableObjectConverter
{
  ...
  public override object ConvertFrom(ITypeDescriptorContext context,
                                     CultureInfo culture, object value)
  {
  GridItem gridItem = context as GridItem;
  object itemValue = gridItem == null ? null : gridItem.Value;

  if (value is string)
      return typeof(T).ConvertFromString((string)value, (T)itemValue);

  return base.ConvertFrom(context, culture, value);
  }
}

So that this general-purpose MyClassConverter can be used by other classes that also provide a static ConvertFromString method.

My problem (so far) is this line of code,

return typeof(T).ConvertFromString((string)value, (T)itemValue);

generates compiler error CS1061. I'm stumped how to fix it.

Side note: this compiles without error,

if (value is string && typeof(T) == typeof(MyClass))
    return MyClass.ConvertFromString((string)value, (MyClass)itemValue);

But I don't want to specify MyClass, I only want to specify T.

My questions:

  1. Is there a way to call the static method ConvertFromString using just T?
  2. Can #1 take advantage of ConvertFromString being static?
  3. Can #2 be done without creating an object of type T beforehand?

Presently, I have many custom TypeConverter classes (none using generics) where the only line of code which differs is that call to ConvertFromString. In each custom TypeConverter, ConvertFromString is explicitly hardcoded with the appropriate class name it converts. How lovely if I could replace all these custom converter classes with a single general purpose converter class.

  • Is this possible?
  • What knowledge am I missing?

(The solution must be backwards compatible to .NET 3.5.)

dathompson
  • 65
  • 3

1 Answers1

0

Thanks Alex!

Yep, MethodInfo/GetMethod was the missing piece.

My general purpose TypeConverter class now looks like this,

public class MyClassConverter : ExpandableObjectConverter
{
    ...
    public override object ConvertFrom(ITypeDescriptorContext context,
                                       CultureInfo culture, object value)
    {
        GridItem gridItem = context as GridItem;
        object itemValue = gridItem == null ? null : gridItem.Value;

        if (value is string)
        {
            MethodInfo m = itemValue.GetType().GetMethod("ConvertFromString");
            return m.Invoke(null, new object[] { value, itemValue });
        }

        return base.ConvertFrom(context, culture, value);
    }
}

Now every class with expandable properties can use a single general purpose TypeConverter class (as long it defines a static ConvertFromString method).

I just eliminated over half dozen custom TypeConverter classes; this new general purpose converter is plenty sufficient for my needs. Sweet!

dathompson
  • 65
  • 3