使用custom属性将值映射到另一个类型

时间:2015-03-30 04:45:06

标签: c# reflection mapping custom-attributes

我正在尝试将我的MVVM中的各种模型派生出来的抽象类组合在一起。这部分内容用于抽象IEditableObject / IChangeTracking细节。

对于我的IEditableObject,我想存储一个浅层的“核心数据”(一个定义将被序列化的数据的结构,通常用于数据库存储),以便可以取消或提交它。我不想做的就是为我提出的每个新模型输入它。

我已经定义了一个我想在派生类的适用属性上使用的[DataCoreItem]自定义属性。出于某些不相关的原因,抽象类采用通用DataCoreType和IDType:

    public abstract class ModelObject<T, DataCoreType, IDType> : INotifyPropertyChanged, IEditableObject
{

    public abstract DataCoreType Load(IDType id);
    public abstract bool Save(DataCoreType dataCore);
    public abstract bool Delete(IDType id);
    // etc...

以下是我的CompanyModel的示例 数据核心:

public struct CompanyDataCore
{
    public int? ID;
    public string Code;
    public string Name;
    public string PrimaryWebsite;
    public string PrimaryPhone;
    public string PrimaryEmail;
}

派生类:

public class CompanyModel : ModelObject<CompanyModel, CompanyDataCore, int> {

    CompanyDataCore dataCore;

    [DataCoreMember]
    public int? ID { get { return dataCore.ID; } set { SetProperty(ref dataCore.ID, value); } }
    [DataCoreMember]
    public string Name { get { return dataCore.Name; } set {SetProperty(ref dataCore.Name, value); } }
    [DataCoreMember]
    public string Code { get { return dataCore.Code; } set { SetProperty(ref dataCore.Code, value); } }
    [DataCoreMember]
    public string PrimaryPhone { get { return dataCore.PrimaryPhone; } set {SetProperty(ref dataCore.PrimaryPhone, value); } }
    [DataCoreMember] 
    public string PrimaryEmail { get { return dataCore.PrimaryEmail; } set { SetProperty(ref dataCore.PrimaryEmail, value); } }
    [DataCoreMember]
    public string PrimaryWebsite { get { return dataCore.PrimaryWebsite; } set { SetProperty(ref dataCore.PrimaryWebsite, value); } }

好的,最后......我想要的抽象类是使用BeginEdit(),EndEdit()和CancelEdit()方法来自动处理数据核心备份副本的存储。以下是我设想的方式:

[DataCoreMember(MemberName="ID")]
public int? ID { get { return dataCore.ID; } set { SetProperty(ref dataCore.ID, value); } }
// etc etc

在我的抽象类中:

    public virtual void BeginEdit() {

        Type t = typeof(T);

        var props = t.GetProperties().Where(
            prop => Attribute.IsDefined(prop, typeof(DataCoreMemberAttribute)));


        // WHAT TO DO HERE???  everything else looks good up to here
        foreach (object o in props) {
            this.dataCoreBackup.???? = o.value;
        }

        IsEditing = true;
    }

如何将DataCoreMember应用的属性映射到指定的struct属性?

我缺乏反思经验(以及工作通用类型),但我认为这可以做到。我找到了如何获取应用了属性的属性列表的示例(尚未尝试过),但我不确定如何最终根据它来引用DataCore的属性。谁能告诉我怎么样?非常感谢。

1 个答案:

答案 0 :(得分:0)

事实证明,使用Reflection是一项相当容易的任务。下面的解释(大部分非相关代码被删除)

这包含数据备份,因此支持CancelEdit:

public struct CompanyDataCore
{
    public int? ID;
    public string Code;
    public string Name;
    public string PrimaryWebsite;
    public string PrimaryPhone;
    public string PrimaryEmail;
    public string RootPath;
}

这是属性类,用于表示备份哪些字段:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class DataCoreMemberAttribute : Attribute
{
    public string MemberName { get; set; }
}

这是派生类:

public class CompanyModel : ModelObject<CompanyModel, CompanyDataCore, int>
{

    [Identifier]
    [DataCoreMember(MemberName="ID")]
    public int? ID { get { return dataCore.ID; } set { SetProperty(ref dataCore.ID, value); } }

    [DataCoreMember(MemberName="Name")]
    public string Name { get { return dataCore.Name; } set {SetProperty(ref dataCore.Name, value); } }

    [DataCoreMember(MemberName="Code")]
    public string Code { get { return dataCore.Code; } set { SetProperty(ref dataCore.Code, value); } }

    [DataCoreMember(MemberName="PrimaryPhone")]
    public string PrimaryPhone { get { return dataCore.PrimaryPhone; } set {SetProperty(ref dataCore.PrimaryPhone, value); } }

    [DataCoreMember(MemberName="PrimaryEmail")] 
    public string PrimaryEmail { get { return dataCore.PrimaryEmail; } set { SetProperty(ref dataCore.PrimaryEmail, value); } }

    [DataCoreMember(MemberName="PrimaryWebsite")]
    public string PrimaryWebsite { get { return dataCore.PrimaryWebsite; } set { SetProperty(ref dataCore.PrimaryWebsite, value); } }

}

这是抽象类:

public abstract class ModelObject<T, DataCoreType, IDType> : INotifyPropertyChanged, IEditableObject
{
    protected DataCoreType dataCoreBackup;

    public virtual void BeginEdit() {

        Type t = typeof(T);

        // get a the properties with the attribute
        var props = t.GetProperties().Where(
            prop => Attribute.IsDefined(prop, typeof(DataCoreMemberAttribute)));

        // backup needs to be boxed because it's a struct
        object boxedBackup = this.dataCoreBackup;

        foreach (var prop in props) {

            foreach (CustomAttributeData attribData in prop.GetCustomAttributesData()) {

                if (attribData.Constructor.DeclaringType == typeof(DataCoreMemberAttribute)) {

                    object origValue = prop.GetValue(this);
                    FieldInfo field = boxedBackup.GetType().GetField(attribData.NamedArguments[0].TypedValue.Value.ToString());
                    field.SetValue(boxedBackup, origValue);
                }
            }
        }

        this.dataCoreBackup = (DataCoreType)boxedBackup;

        IsEditing = true;
    }

...现在我可以在一个抽象类中处理INotifiyPropertyChanged和IEditbaleObject,所以我不必在每个特定模型中编写一堆管道,我将要使用它。

希望其他人能发现这很有用。