简体   繁体   中英

Generic type converter - issues with generics

I'm trying to get this working: http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/

I took code from comments where we have base class and then derive from it. However I get error when I have it like this:

public abstract class ConverterMarkupExtension: MarkupExtension, IValueConverter where T: class, new()
    {
        private static T m_converter = null;

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return m_converter ?? (m_converter = new T());
        }

        #region IValueConverter Members

            public abstract object Convert(object value, Type targetType, object parameter, CultureInfo culture);
            public abstract object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);

        #endregion
    }

It says "constraint are not allowed on non-generic declarations. I change it to:

public abstract class ConverterMarkupExtension<T>: MarkupExtension, IValueConverter where T: class, new()

and this seems to fix it but then I can't derive from this class

public class BytesToKBValueConverter : ConverterMarkupExtension
    {
        public override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is double)
            {
                double bytes = System.Convert.ToDouble(value);
                return bytes / 1024.0d;
            }
            else
                return value;
        }

        public override object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

Here I get error:

Error 1 Using the generic type 'IDATT.Dispatch.ConverterMarkupExtension' requires 1 type arguments C:\SVN\IDATT\Code\IDATT.Dispatch\ValueConverters.cs 29 44 IDATT.Dispatch

Also, can somebody explain what "where T: class, new" means? I'm just starting to understand generics but not sure what this particular syntax does.

Just so you understand, you are implementing a suggested generic extension to Dr. WPF's technique described in his article. That extension was provided in a comment to the article by AlanO. Because AlanO tried to paste code directly into a comment, all greater than and less than characters were "eaten" rendering his code non-compilable.

So for example, the commenter pasted this code in to the comment:

public abstract class ConverterMarkupExtension<T>: MarkupExtension, IValueConverter

and this is what made it through the comment filter:

public abstract class ConverterMarkupExtension: MarkupExtension, IValueConverter

As a result, you have to repair the damaged code to get it working again. If you are not comfortable with generics, what to repair might not be immediately obvious!

I'm guessing the next thing to repair is:

public class BytesToKBValueConverter : ConverterMarkupExtension

which should be:

public class BytesToKBValueConverter : ConverterMarkupExtension<BytesToKBValueConverter>

If this is your first exposure to generics, this style of is on the unusual side!

The effect is that the code is a value converter and a markup extension at the same time and the markup converter just returns another instance of itself (which is a value converter). However, this is the spirit of the original article but my opinion is that Dr. WPF's original non-generic approach is tricky enough without introducing generics that just make the code harder to read without really saving much boilerplate. It's up to you which approach better meets your needs.

Where T : class, new()

Says the type T should be a reference type and it should have a public parameterless ctor.

public class BytesToKBValueConverter : ConverterMarkupExtension

Please provide ConverterMarkeupExtension< T > T type there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM