asp.net: useful things, part two

    Attributes are a useful thing, which is one of the main mechanisms of the .net framework. In this article, I propose to consider one simple example of the use of attributes. This article continues the previous article and uses the extension methods defined there.

    Sometimes there is a need to check at once some validity of the state of an object. For example, you may need to be sure that all necessary fields and properties of an object are initialized and not null. To solve this problem, attributes are applicable.

    Here is a simple example of an attribute declaration:
        [AttributeUsage (AttributeTargets.Field | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
        sealed public class CheckNullAttribute: Attribute
        {
            public CheckNullAttribute ()
            {
            }
        }
    


    An attribute is declared for fields and properties that will not be inherited, and will not be able to be set several times. To process this attribute, some logic is required, which I give below (here we use one of the extension methods discussed in the first article):
        public class CheckNullMembers
        {
            public static void Check (Type p_type, object p_obj)
            {
                if (p_type == null || p_obj == null)
                    throw new Exception (“Invalid parameters”);
                MemberInfo [] _members = p_type.FindMembers (MemberTypes.Field | MemberTypes.Property,
                    BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
                    null, null);
                foreach (MemberInfo member in _members)
                {
                    bool _isDef = Attribute.IsDefined (member, typeof (CheckNullAttribute));
                    if (_isDef)
                    {
                        if (member.GetValue (p_obj) == null)
                            throw new Exception (String.Format ("{0} is not initialized and is null", member.Name));
                    }
                }
            }
        }


    Where is this applicable? As you know, when developing a page or control on aps.net, you can declare a public property and initialize it in the markup, something like this:
        public int? Branchid
        {
            get;
            set;
        }
        


    Now apply the attribute for this code and add the verification logic:
        [CheckNull ()]
        public int? Branchid
        {
            get;
            set;
        }
        protected void Page_Load (object sender, EventArgs e)
        {
            if (this.Visible)
            {
                CheckNullMembers.Check (this.GetType (). BaseType, this);
            }
        }
    


    In this code, BranchId will be checked for null, and if BranchId does not matter, an exception will be thrown.

    Comment: The type and instance of the class are passed to Check. This is done due to the fact that at runtime the page type cannot be obtained from this; asp.net wraps each page in the ASP namespace. All private fields of the page will not be visible if you take the type through this.GetType (). In this regard, the programmer will have to explicitly specify which type to process. Perhaps the solution is not perfect, I will be glad to hear alternative options.

    Also popular now: