在 VS10 .NET 形式中寻找更动态的设计器 TypeConverter 序列化 [英] Looking for a more dynamic designer TypeConverter serialization in a VS10 .NET form

查看:16
本文介绍了在 VS10 .NET 形式中寻找更动态的设计器 TypeConverter 序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类的列表,我使用以下代码将其序列化为设计器生成的代码:

I have a list of a class which I serialize to the designer generated code with the following code:

internal class TargetSettingsConverter : TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(InstanceDescriptor) && value is ControlListManager.TargetSettings)
        {
            ConstructorInfo constructor = typeof(ControlListManager.TargetSettings).GetConstructor(new[] { typeof(object), typeof(string), typeof(DisplayModes), typeof(bool), typeof(int), typeof(int), typeof(int) });
            var target = value as ControlListManager.TargetSettings;
            var descriptor = new InstanceDescriptor(constructor, new[] { target.Target, target.Text, target.DisplayMode, target.Fade, target.HideTimeout, target.PaddingX, target.PaddingY }, true);
            return descriptor;
        }
        if (culture == null) { culture = CultureInfo.CurrentCulture; }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}

到目前为止效果很好,但让我烦恼的是我必须单独指定类型和值.

This works well so far, but what bothers me is that I have to specify the types and values individually.

我的第一个想法是使用 GetConstructors() 代替.但随后的价值观问题仍然存在.我真的对这个问题感到困惑,因为我实际上在不知道参数数量的情况下编写函数 - 或者仅当它是硬编码"时.

My first idea was to use GetConstructors() instead. But then to problem of the values still remains. I'm really confused about this problem since I actually write the function without knowing the amount of parameters - or only if it is "hardcoded".

有人知道如何可以做得更好吗?

Does anyone have any idea how this can be done better?

数据类

    [TypeConverter(typeof(TargetSettingsConverter))]
    public class TargetSettings : IEquatable<TargetSettings>
    {
        public object Target = new { };
        public string Text;
        public DisplayModes DisplayMode = DisplayModes.FollowXY;
        public bool Fade = true;
        public int HideTimeout;
        public int PaddingX;
        public int PaddingY;
        public bool Equals(TargetSettings other)
        {
            return other != null && (Target.Equals(other.Target));
        }
        public override bool Equals(object obj)
        {
            if (obj == null) { return false; }
            TargetSettings objAsPart = obj as TargetSettings;
            if (objAsPart == null) { return false; }
            return Equals(objAsPart);
        }
        public override int GetHashCode()
        {
            return Target.GetHashCode();
        }
        public TargetSettings(object target, string text = "", DisplayModes displayMode = DisplayModes.FollowXY, bool fade = true, int hideTimeout = 0, int paddingX = 0, int paddingY = 0)
        {
            Target = target;
            Text = text;
            DisplayMode = displayMode;
            Fade = fade;
            HideTimeout = hideTimeout;
            PaddingX = paddingX;
            PaddingY = paddingY;
        }
    }

推荐答案

根据您的问题,您希望针对类型和值使代码更加动态.

Based on your question, you want to make your code more dynamic for the types and values.

因为你要转换的类型和值都是字段类型.我建议你使用反射来做.

Since the types and values you want to convert are all field types. I recommend that you use the reflection to do it.

您可以获得以下所有字段类型:

You can get all the field types as the following:

 Type []tarr = typeof(TargetSettings).GetFields().Select(i => i.FieldType).ToArray();
 ConstructorInfo constructor = typeof(TargetSettings).GetConstructor(tarr);

您可以按如下方式获取所有字段值:

You can get all the field values as the following:

object []oarr= typeof(TargetSettings).GetFields().Select(i => i.GetValue(target)).ToArray();
var descriptor = new InstanceDescriptor(constructor, oarr, true);

完整代码:

internal class TargetSettingsConverter : TypeConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(InstanceDescriptor) && value is TargetSettings)
            {
                Type []tarr = typeof(TargetSettings).GetFields().Select(i => i.FieldType).ToArray();
                ConstructorInfo constructor = typeof(TargetSettings).GetConstructor(tarr);
                var target = value as TargetSettings;
                object []oarr= typeof(TargetSettings).GetFields().Select(i => i.GetValue(target)).ToArray();
                var descriptor = new InstanceDescriptor(constructor, oarr, true);
                return descriptor;
            }
            if (culture == null) { culture = CultureInfo.CurrentCulture; }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }


    [TypeConverter(typeof(TargetSettingsConverter))]
    public class TargetSettings : IEquatable<TargetSettings>
    {
        public object Target = new { };
        public string Text;
        public DisplayModes DisplayMode = DisplayModes.FollowXY;
        public bool Fade = true;
        public int HideTimeout;
        public int PaddingX;
        public int PaddingY;
        public bool Equals(TargetSettings other)
        {
            return other != null && (Target.Equals(other.Target));
        }
        public override bool Equals(object obj)
        {
            if (obj == null) { return false; }
            TargetSettings objAsPart = obj as TargetSettings;
            if (objAsPart == null) { return false; }
            return Equals(objAsPart);
        }
        public override int GetHashCode()
        {
            return Target.GetHashCode();
        }
        public TargetSettings(object target, string text = "", DisplayModes displayMode = DisplayModes.FollowXY, bool fade = true, int hideTimeout = 0, int paddingX = 0, int paddingY = 0)
        {
            Target = target;
            Text = text;
            DisplayMode = displayMode;
            Fade = fade;
            HideTimeout = hideTimeout;
            PaddingX = paddingX;
            PaddingY = paddingY;
        }
    }

    public enum DisplayModes
    {
        FollowXY

    }

这篇关于在 VS10 .NET 形式中寻找更动态的设计器 TypeConverter 序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆