【问题标题】:c# keeping custom attributes in separate assemblyc# 将自定义属性保存在单独的程序集中
【发布时间】:2016-07-10 15:52:16
【问题描述】:

我有自定义属性和使用这些属性的类。选择类对象时,这些属性用于属性网格。目前,类和属性都在同一个程序集中。在属性中,我有一些 Form 对象。由于这些 Form 对象,我想将属性保存在单独的程序集中。但是,它会导致循环引用。你能帮我解决这个问题吗?

示例:

我有一个业务对象,它的属性可以在 PropertyGridControl 中显示:

    public class Field
    {
        public Field()
        {

        }

        private int _Type;

        [CustomPropertyEditorMarker(typeof(RepositoryItemForFieldDataType))]
        public int Type
        {
            get { return _Type; }
            set
            {
                _Type = value;
            }
        }
    }

    [AttributeUsage(AttributeTargets.Property)]
    public sealed class CustomPropertyEditorMarker : Attribute
    {
        public CustomPropertyEditorMarker(Type editorType)
        {
            EditorType = editorType;
        }

        public readonly Type EditorType;
    }

    public sealed class RepositoryItemForFieldDataType : RepositoryItemLookUpEdit
    {
        public RepositoryItemForFieldDataType()
        {
                // Populating LookupEdit details here
        }

        private void On_ButtonClick()
        {
            // Here initializing existing Form class and show it
        }
    }

When Field object is selected, PropertGridControl analyze selected object and checking which property has above Attribute. If yes, then initialize it.

        private void SelectObject(object obj)
        {
            this.Rows.Clear();
            this.DefaultEditors.Clear();
            this.RepositoryItems.Clear();

            if ((this.LastSelectedObject as ApplicationDomainItemBase) != null)
            {
                (this.LastSelectedObject as ApplicationDomainItemBase).IsSelected = false;
            };

            this.SelectedObject = null;

            this.SelectedObject = obj;

            if (!(this.SelectedObject is ConfigurationObjectManagerBase))
            {
                foreach (var propInfo in this.SelectedObject.GetType().GetProperties())
                {
                    object[] objFieldAtts = propInfo.GetCustomAttributes(typeof(CustomPropertyEditorMarker), true);

                    if (objFieldAtts != null && objFieldAtts.Length > 0)
                    {
                        if (this.GetRowByFieldName(propInfo.Name) != null)
                        {
                            RepositoryItem repItem = Activator.CreateInstance(((CustomPropertyEditorMarker)objFieldAtts[0]).EditorType) as RepositoryItem;
                            this.GetRowByFieldName(propInfo.Name).Properties.RowEdit = repItem;
                        };
                    };
                };
            };

            this.LastSelectedObject = obj;
        }

目前业务对象类和属性都在同一个程序集中,需要将它们分开。但是我不能,因为业务对象属性是用属性名称装饰的,需要添加引用。无法添加引用,因为属性类引用了业务对象类。希望清楚。谢谢。

【问题讨论】:

  • 为什么要移动属性的项目引用使用这些属性的项目?您能否提供有关您的依赖项的更多详细信息?
  • @user3185569,请参阅编辑。谢谢。
  • 我在您的示例代码中只看到一个属性类,它没有引用任何其他类。我错过了什么。我看不出是什么阻止您将该属性类移动到单独的程序集,然后从包含您的域对象的项目中引用该程序集。

标签: c# .net-assembly


【解决方案1】:

没有看到导致您的问题的业务对象引用,一般答案如下:

将您的业务对象建立在接口上,这些接口可以在与您要移动属性的位置相同的程序集中声明,也可以在另一个“基础”程序集中声明。然后通过接口引用属性中的业务对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多