c#将自定义属性保存在单独的程序集中

时间:2016-07-10 15:52:17

标签: c# .net-assembly

我有自定义属性和使用这些属性的类。选择类对象时,这些属性用于Property Grid。目前,类和属性都在同一个程序集中。在属性中,我有一些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;
        }

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

1 个答案:

答案 0 :(得分:0)

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

将您的业务对象基于接口,这些接口将在您想要移动属性的同一程序集中声明,或者另一个" base"部件。然后通过接口引用属性中的业务对象。