简体   繁体   English

C#绑定数据对象

[英]C# Binding data objects

I need binding between two similar objects (C#): 我需要在两个相似的对象(C#)之间进行绑定:

public class TypeA
{
     public int I;
     public string S;
}

public class TypeB
{
     public IntField I;
     public StringField S;
}

When a field in TypeA changes I need to update the matching field in TypeB. 当TypeA中的字段更改时,我需要更新TypeB中的匹配字段。

IntField is an object that has a Value field of int type, so that updating TypeB can be written as: IntField是一个具有int类型的Value字段的对象,因此更新TypeB可以写为:

bInstance.I.Value = aInstance.I;

If I understand correctly, if I use INotifyPropertyChanged in order to bind TypeB to TypeA, it'll cause boilerplate: 如果我理解正确,如果我使用INotifyPropertyChanged以便将TypeB绑定到TypeA,则会导致样板:

aInstance.PropertyChanged += (sender, args) =>
{
    if (args.PropertyName == "I")
        this.I.Value = sender.I;
    if (args.PropertyName == "S")
        this.S.Value = sender.S;
};

Also: 也:

  • I have access to the code in both types, and I'd rather not change TypeB. 我可以访问两种类型的代码,而我不想更改TypeB。
  • I have ~15 pairs of types like TypeA and TypeB - I'd like to avoid boilerplate. 我有大约15对类型,例如TypeA和TypeB-我想避免样板。
  • Performance is very important, so reflection is not a preferred option. 性能非常重要,因此反射不是首选。
  • Perhaps static reflection is an option? 也许静态反射是一种选择? I've heard about it, but I'm not sure about: 我听说过,但是不确定:
    • How to use it without boilerplate. 如何在没有样板的情况下使用它。
    • Its performance. 其性能。
    • Using it for different instances of pairs of the same type (ie a1Instance->b1Instance, a2Intance->b2Instance, etc.). 将其用于相同类型对的不同实例(即a1Instance-> b1Instance,a2Intance-> b2Instance等)。

Edit: 编辑:

IntField is a class. IntField是一个类。 It's used for another type of data binding that exists in the system (complex, and the entire system relies on this). 它用于系统中存在的另一种数据绑定类型(复杂,整个系统都依赖于此)。 It inherits from a class that represents a general bindable field. 它从表示常规可绑定字段的类继承。 here's part of it: 这是它的一部分:

public class IntField : GeneralField
{
    private int _value;
    public int Value
    {
        get { return _value; }
        set
        {
            IsDirty = true;
            _value = value;
        }
    }

    // ... a couple of abstract method implementations go here (setting _value, and getting value in a non-type specific way)
}

If you don't want lots of manual coding, something reflection-based or meta-programming-based is going to be your best bet. 如果您不希望进行大量手动编码,那么基于反射或基于元编程的方法将是您的最佳选择。 For example: 例如:

static void Entwine(INotifyPropertyChanged source, object target)
{
    source.PropertyChanged += (sender,args) =>
    {
        var prop = target.GetType().GetProperty(args.PropertyName);
        if(prop != null)
        {
            var field = prop.GetValue(target) as GeneralField;
            if(field != null)
            {
                var newVal = source.GetType().GetProperty(args.PropertyName)
                                   .GetValue(source);
                field.SetValue(newVal); // <=== some method on GeneralField
            }
        }
    };
}

In many cases this will be fine, but if the reflection is genuinely a problem, tools like FastMember can help: 在许多情况下,这很好,但是如果确实存在反射问题,则诸如FastMember之类的工具可以为您提供帮助:

static void Entwine(INotifyPropertyChanged source, object target)
{
    var sourceAccessor = ObjectAccessor.Create(source);
    var targetAccessor = ObjectAccessor.Create(target);
    source.PropertyChanged += (sender, args) =>
    {
        var field = targetAccessor[args.PropertyName] as GeneralField;
        if (field != null)
        {
            var newVal = sourceAccessor[args.PropertyName];
            field.SetValue(newVal);
        }
    };
}

This is significantly faster than reflection - it uses a lot of tricks to avoid pain. 这比反射要快得多-它使用很多技巧来避免痛苦。 That just leaves the need for something like: 这仅需要以下内容:

abstract class GeneralField
{
    // ...
    public abstract void SetValue(object value);
}
class Int32Field : GeneralField
{
    // ...
    public override void SetValue(object value)
    {
        Value = (int)value;
    }
}

And of course your INotifyPropertyChanged implementation, for example: 当然还有您的INotifyPropertyChanged实现,例如:

public class TypeA : INotifyPropertyChanged
{
    private int i;
    private string s;
    public int I
    {
        get { return i; }
        set { SetField(ref i, value); }
    }
    public string S
    {
        get { return s; }
        set { SetField(ref s, value); }
    }
    private void SetField<T>(ref T field, T value,
        [CallerMemberName]string propertyName = null)
    {
        if (!EqualityComparer<T>.Default.Equals(field, value))
        {
            field = value;
            var handler = PropertyChanged;
            if (handler != null) handler(
                this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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