/// <summary>
 /// Sets the data (no logic involved) either with field or with a property
 /// </summary>
 /// <param name="name">name of the property or the field</param>
 /// <param name="superclass">Member is in the Superclass (optional); null for the current class</param>
 /// <param name="value"></param>
 public void SetData(string name, object value, out ClassMemberType memberType, Class superclass = null)
 {
     try
     {
         memberType = ClassMemberType.Property;
         Set(name, value, memberType, superclass);
     }
     catch (MissingMemberException)
     {
         memberType = ClassMemberType.Field;
         Set(name, value, memberType, superclass);
     }
 }
Пример #2
0
        public void GetMemberTypeTest()
        {
            string          name;
            ClassMemberType type = DocInfo.GetMemberType("M:This.Is.A.Method", out name);

            Expect.IsTrue(type == ClassMemberType.Method);
            Expect.AreEqual("This.Is.A.Method", name);
            type = DocInfo.GetMemberType("F:This.Is.A.Field", out name);
            Expect.IsTrue(type == ClassMemberType.Field);
            Expect.AreEqual("This.Is.A.Field", name);
            type = DocInfo.GetMemberType("T:This.Is.A.Type", out name);
            Expect.IsTrue(type == ClassMemberType.Type);
            Expect.AreEqual("This.Is.A.Type", name);
            type = DocInfo.GetMemberType("P:This.Is.A.Property", out name);
            Expect.IsTrue(type == ClassMemberType.Property);
            Expect.AreEqual("This.Is.A.Property", name);
        }
        /// <summary>
        /// Sets the value to a member
        /// </summary>
        /// <param name="name">member's name</param>
        /// <param name="value">value to set</param>
        /// <param name="memberType">member type (either: field, property, or method)</param>
        /// <param name="superclass">super class for polymorphic creation</param>
        public void Set(string name, object value, ClassMemberType memberType, Type type)
        {
            switch (memberType)
            {
            case ClassMemberType.Field:
                var field = type.GetField(name);
                if (field == null)
                {
                    throw new MissingFieldException($"The field \"{name}\" of {this} does not exist or is inaccessible (non-public).");
                }
                if (!field.IsPublic || field.IsInitOnly)
                {
                    throw new MemberAccessException($"The field \"{name}\" of {this} must be public and not be readonly.");
                }
                field.SetValue(_object, value);
                break;

            case ClassMemberType.Property:
                var property = type.GetProperty(name);
                if (property == null)
                {
                    throw new MissingMemberException($"The property \"{name}\" of {type.Name} does not exist.");
                }
                if (property.GetSetMethod(true) == null || !property.CanWrite)
                {
                    throw new MemberAccessException($"The property \"{name}\" of {type.Name} cannot be set.");
                }
                property.SetValue(_object, value);
                break;

            case ClassMemberType.Method:

                var method = type.GetMethod(name);
                if (method == null)
                {
                    throw new MissingMemberException($"The method obj.{name}(value) of {type.Name} does not exist or is inaccessible (non-public).");
                }
                method.Invoke(_object, new object[] { value });
                break;

            default:
                throw new ApplicationException($"The member type {memberType} is unknown.");
            }
        }
 /// <summary>
 /// Sets the data (no logic involved) using a field or a property
 /// </summary>
 /// <param name="name"></param>
 /// <param name="isProperty">Returns true if it's a property.</param>
 /// <param name="superclass">Member is in the Superclass (optional); null for the current class</param>
 /// <returns></returns>
 public dynamic GetData(string name, out ClassMemberType memberType, Class superclass = null)
 {
     try
     {
         memberType = ClassMemberType.Field;
         return(Get(name, memberType, superclass));
     }
     catch (MissingFieldException)
     {
         try
         {
             memberType = ClassMemberType.Property;
             return(Get(name, memberType, superclass));
         }
         catch (MissingMemberException)
         {
             throw new MissingMemberException($"The data member \"{name}\" of {this} (field or property) does not exist or is inaccessible (non-public).");
         }
     }
 }
        /// <summary>
        /// obj.Get() or obj.Get
        /// </summary>
        /// <param name="name">name of the member (property or method)</param>
        /// <param name="memberType">whether this is using a method or a property</param>
        /// <param name="superclass">Member is in the Superclass (optional); null for the current class</param>
        public dynamic Get(string name, out ClassMemberType memberType, Class superclass = null)
        {
            Type type = superclass == null ? Type : superclass.Type;

            try
            {
                memberType = ClassMemberType.Property;
                return(Get(name, memberType, superclass));
            }
            catch (MissingMemberException)
            {
                try
                {
                    memberType = ClassMemberType.Method;
                    return(Get(name, memberType, superclass));
                }
                catch (MissingMethodException)
                {
                    throw new MissingMemberException($"The member obj.{name}() <method> or obj.{name} <property> of {this} does not exist or is inaccessible (non-public).");
                }
            }
        }
        /// <summary>
        /// Gets the value from a member
        /// </summary>
        /// <param name="name">member name</param>
        /// <param name="memberType">member type (either field, property, or method)</param>
        /// <param name="type">polymorphic type</param>
        /// <returns></returns>
        public dynamic Get(string name, ClassMemberType memberType, Type type)
        {
            switch (memberType)
            {
            case ClassMemberType.Field:
                var field = type.GetField(name);
                if (field == null)
                {
                    throw new MissingFieldException($"The field \"{name}\" of {this} does not existor or is inaccessible (non-public).");
                }
                if (!field.IsPublic)
                {
                    throw new MemberAccessException($"The field \"{name}\" of {this} must be public.");
                }
                return(field.GetValue(_object));

            case ClassMemberType.Property:
                var property = type.GetProperty(name);
                if (property == null)
                {
                    throw new MissingMemberException($"The property \"{name}\" of {this}does not exist or is inaccessible (non-public).");
                }
                if (!property.CanRead)
                {
                    throw new MemberAccessException($"The property \"{name}\" of {this} cannot be read.");
                }
                return(property.GetValue(_object));

            case ClassMemberType.Method:
                var method = type.GetMethod(name);
                if (method == null)
                {
                    throw new MissingMemberException($"The method obj.{name}() of {this} <method> does not exist or is inaccessible (non-public).");
                }
                return(method.Invoke(_object, new object[0]));
            }
            throw new ApplicationException($"The member type {memberType} is unknown.");
        }
        /// <summary>
        /// Sets the value to a member
        /// </summary>
        /// <param name="name">member's name</param>
        /// <param name="value">value to set</param>
        /// <param name="memberType">member type (either: field, property, or method)</param>
        /// <param name="superclass">super class for polymorphic creation</param>
        public void Set(string name, object value, ClassMemberType memberType, Class superclass = null)
        {
            Type type = superclass == null ? Type : superclass.Type;

            Set(name, value, memberType, type);
        }
        /// <summary>
        /// Gets the value from a member
        /// </summary>
        /// <param name="name">member name</param>
        /// <param name="memberType">member type (either field, property, or method)</param>
        /// <param name="superclass">super class for polymorphic creation</param>
        /// <returns></returns>
        public dynamic Get(string name, ClassMemberType memberType, Class superclass = null)
        {
            Type type = superclass == null ? Type : superclass.Type;

            return(Get(name, memberType, type));
        }