📜 ⬆️ ⬇️

asp.net: useful things, part one

I want to share some useful things for me that I use in my work. This is the first part of the articles, plans to write a few, developing a consistent theme. I want to note right away that I do not pretend to a perfect code, on the contrary, with your comments, I hope to make my tools even more productive and useful.

The first article is rather introductory, in it I want to define several useful extension methods for the reflection mechanism. These methods will later come in handy when building my tools. This article may be useless for most programmers, so I do not hope for its popularity. But still, I think that the comments and to it will arise too.

As it is known in .net there is a reflection mechanism (reflection), which allows you to receive information about classes during performance. I will not describe what and how and why. Immediately to the point: in the reflections there are several similar classes FieldInfo, ProppertyInfo and some unifying (base) class MemberInfo. When we program attributes for fields and properties at once, then for processing we can consistently select fields and then properties or get everything at once through MemberInfo. I stopped at the last version, but for obvious reasons, this class is slightly trimmed compared to other classes. For example, it does not have the methods SetValue, GetValue, and I would like to receive the type for the fields and properties immediately. In this regard, I propose the following solution:
 namespace Xaoc.BasicClasses
 {
     public static class ReflectionUtil
     {
         public static void SetValue (this MemberInfo p_member, object obj, object value)
         {
             if (p_member == null || obj == null)
                 throw new Exception ("Invalid parameters");

             switch (p_member.MemberType)
             {
                 case MemberTypes.Field:
                     (p_member as FieldInfo) .SetValue (obj, value);
                     break;
                 case MemberTypes.Property:
                     if ((p_member as PropertyInfo) .CanWrite)
                         (p_member as PropertyInfo) .SetValue (obj, value, null);
                     else
                         throw new Exception ("Property cannot be changed");
                     break;
                 default:
                     throw new Exception ("MemberTypes incompatible type");
             }
         }

         public static object GetValue (this MemberInfo p_member, object obj)
         {
             if (p_member == null || obj == null)
                 throw new Exception ("Invalid parameters");

             object result = null;

             switch (p_member.MemberType)
             {
                 case MemberTypes.Field:
                     result = (p_member as FieldInfo) .GetValue (obj);
                     break;
                 case MemberTypes.Property:
                     if ((p_member as PropertyInfo) .CanRead)
                         result = (p_member as PropertyInfo) .GetValue (obj, null);
                     else
                         throw new Exception ("Unable to get property value");
                     break;
                 default:
                     throw new Exception ("MemberTypes incompatible type");
             }

             return result;
         }

         public static Type GetMemberType (this MemberInfo p_member)
         {
             if (p_member == null)
                 throw new Exception ("Invalid parameters");

             Type result = null;
             switch (p_member.MemberType)
             {
                 case MemberTypes.Field:
                     result = (p_member as FieldInfo) .FieldType;
                     break;
                 case MemberTypes.Property:
                     result = (p_member as PropertyInfo) .PropertyType;
                     break;
                 default:
                     throw new Exception ("MemberTypes incompatible type");
             }
             return result;
         }
     }
 }


These methods allow MemberInfo to set values ​​for fields and properties, to get these values, and also to recognize types. If an attempt is made to set a value to a field or property, an exception is generated.

')

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


All Articles