📜 ⬆️ ⬇️

asp.net: useful things, part two

Attributes are a useful thing, which is one of the basic mechanisms of the .net framework. In this article, I propose to consider one simple example of using 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 the object. For example, you may need to be sure that all the necessary fields and properties of the object are initialized and not null. To solve this problem, attributes are applicable.

Here is a simple attribute declaration example:
     [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 specified multiple times. To process this attribute, some logic is required, which I cite below (here one of the extension methods discussed in the first article is applied):
     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} not initialized and is null", member.Name));
                 }
             }
         }
     } 


Where is it applicable? As you know, when designing a page or control on aps.net, you can declare a public property and initialize it in the markup, like this:
     public int?  Branchid
     {
         get;
         set;
     }
     <uc3: BranchHint ID = "BranchHint1" runat = "server" BranchId = ”12” />


Now apply the attribute for this code and add the validation 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 is not significant, an exception will be raised.

Comment: the type and class instance are passed to Check. This is done due to the fact that at run time, the page type on this cannot be obtained, asp.net wraps each page in the ASP namespace. All private page margins will not be visible if you take the type via 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 alternatives.

')

Source: https://habr.com/ru/post/24891/


All Articles