Beispiel #1
0
 /// <summary> Vrátí hodnotu fieldu nebo propertry objektu. </summary>
 /// <param name="member"> Popis fieldu nebo property. </param>
 /// <param name="obj"> Objekt. </param>
 /// <returns> Hodnota fieldu nebo property. </returns>
 public static object GetMemberValue ( MemberInfo member, object obj )
 {
   if ( member is FieldInfo )
     return ((FieldInfo)member).GetValue( obj );
   else if ( member is PropertyInfo )
     return ((PropertyInfo)member).GetValue( obj, null );
   else
     throw new Exception( string.Format( "OtherCode.GetMemberValue: unsupported member type '{0}', class '{1}'", member.GetType(), obj.GetType() ) );
 }
Beispiel #2
0
 /// <summary> Vrátí typ fieldu nebo property objektu. </summary>
 /// <param name="member"> Popis fieldu nebo property. </param>
 /// <returns> Typ fieldu nebo property. </returns>
 public static Type GetMemberType ( MemberInfo member )
 {
   if ( member is FieldInfo )
     return ((FieldInfo)member).FieldType;
   else if ( member is PropertyInfo )
     return ((PropertyInfo)member).PropertyType;
   else
     throw new Exception( string.Format( "OtherCode.GetMemberType: unsupported member type '{0}'", member.GetType() ) );
 }
Beispiel #3
0
 /// <summary> Nastaví hodnotu fieldu nebo propertry objektu. </summary>
 /// <param name="member"> Popis fieldu nebo property. </param>
 /// <param name="obj"> Objekt. </param>
 /// <param name="value"> Nastavovaná hodnota. </param>
 public static void SetMemberValue ( MemberInfo member, object obj, object value )
 {
   if ( member is FieldInfo )
     ((FieldInfo)member).SetValue( obj, value );
   else if ( member is PropertyInfo )
     ((PropertyInfo)member).SetValue( obj, value, null );
   else
     throw new Exception( string.Format( "OtherCode.SetMemberValue: unsupported member type '{0}', class '{1}', value '{2}'", member.GetType(), obj.GetType(), value.GetType() ) );
 }
		// Compares two members.  The .Equals method on the member
		// does not work since the ReflectedType for the members might
		// be different and we don't care about that.
		internal static bool IsMemberEqual(MemberInfo m1,
										   MemberInfo m2)
		{
			if (!m1.GetType().Equals(m1.GetType()))
				return false;
			if (!m1.DeclaringType.Equals(m2.DeclaringType))
				return false;
			if (!m1.Name.Equals(m2.Name))
				return false;

			// All but constructors and methods are equal at this point
			if (!(m1 is MethodBase))
				return true;

			// The method handles check that the methods are actually
			// the same
			if (((MethodBase)m1).MethodHandle.
				Equals(((MethodBase)m2).MethodHandle))
				return true;
			return false;
		}
 internal Type GetMemberType(MemberInfo objMember)
 {
     if (objMember is FieldInfo)
     {
         return ((FieldInfo) objMember).FieldType;
     }
     if (!(objMember is PropertyInfo))
     {
         throw new SerializationException(Environment.GetResourceString("Serialization_SerMemberInfo", new object[] { objMember.GetType() }));
     }
     return ((PropertyInfo) objMember).PropertyType;
 }
 internal Type GetMemberType(MemberInfo objMember)
 {
     if (objMember is FieldInfo)
     {
         return ((FieldInfo) objMember).FieldType;
     }
     if (!(objMember is PropertyInfo))
     {
         throw new SerializationException(string.Format(CultureInfo.CurrentCulture, SoapUtil.GetResourceString("Serialization_SerMemberInfo"), new object[] { objMember.GetType() }));
     }
     return ((PropertyInfo) objMember).PropertyType;
 }
Beispiel #7
0
        public static MemberAccessor Create(MemberInfo memberInfo, bool requiresSetter)
        {
            FieldInfo fieldInfo;
            PropertyInfo propertyInfo;
            if ((fieldInfo = memberInfo as FieldInfo) != null)
            {
                return new FieldMemberAccessor(fieldInfo);
            }
            else if ((propertyInfo = memberInfo as PropertyInfo) != null)
            {
                return new PropertyMemberAccessor(propertyInfo, requiresSetter);
            }

            throw new NotSupportedException(memberInfo.GetType().ToString());
        }
Beispiel #8
0
        internal MemberMapInfo(MemberInfo member, string columnName, SqlType columnType, bool nullable, ConstraintMapInfo[] constraints)
        {
            Member = member;
            ColumnName = columnName;
            ColumnType = columnType;
            IsNullable = nullable;
            Constraints = constraints;

            if (member is PropertyInfo) {
                MemberType = ((PropertyInfo) member).PropertyType;
            } else if (member is FieldInfo) {
                MemberType = ((FieldInfo) member).FieldType;
            } else {
                throw new ArgumentException(String.Format("Member of type '{0}' is not permitted.", member.GetType()));
            }
        }
        public static IReflectionAccessor Create(MemberInfo memberInfo)
        {
            var propInfo = memberInfo as PropertyInfo;
            if (propInfo != null)
            {
                return new PropertyInfoAccessor(propInfo);
            }

            var fieldInfo = memberInfo as FieldInfo;
            if (fieldInfo != null)
            {
                return new FieldInfoAccessor(fieldInfo);
            }

            throw new ArgumentException("invalid member info:" + memberInfo.GetType());
        }
        protected static Type GetMemberType(MemberInfo memberInfo)
        {
            var propertyInfo = memberInfo as PropertyInfo;
            var fieldInfo = memberInfo as FieldInfo;

            if (propertyInfo != null)
            {
                return propertyInfo.PropertyType;
            }

            if (fieldInfo != null)
            {
                return fieldInfo.FieldType;
            }

            throw new NotSupportedException(string.Format("{0} not supported", memberInfo.GetType().Name));
        }
        /// <summary>
        /// Gets the object member value.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="member">The member.</param>
        /// <returns>The value of object member.</returns>
        /// <exception cref="ArgumentException">If <paramref name="member"/> is of unknown type.</exception>
        public object GetObjectMemberValue(object data, MemberInfo member)
        {
            object ret;
            if (member is PropertyInfo)
            {
                PropertyInfo propInfo = (PropertyInfo)member;
                ret = propInfo.GetValue(data, null);
            }
            else if (member is FieldInfo)
            {
                FieldInfo fieldInfo = (FieldInfo)member;
                ret = fieldInfo.GetValue(data);
            }
            else
            {
                // Unknown member type - throw it away
                string message = "Unknown type of member: " + (null == member ? "(null)" : member.GetType().FullName);
                Log.Error(message);
                throw new ArgumentException(message, "member");
            }

            return ret;
        }
Beispiel #12
0
        public static bool IsNullable(this MemberInfo member)
        {
            // https://stackoverflow.com/questions/58453972/how-to-use-net-reflection-to-check-for-nullable-reference-type
            var type = member.GetType();

            if (type.IsValueType)
            {
                return(Nullable.GetUnderlyingType(type) != null);
            }
            var nullable = member.CustomAttributes.FirstOrDefault(x => x.AttributeType.FullName == NullableAttributeFullName);

            if (nullable != null && nullable.ConstructorArguments.Count == 1)
            {
                var attributeArgument = nullable.ConstructorArguments[0];
                if (attributeArgument.ArgumentType == typeof(byte[]))
                {
                    var args = (ReadOnlyCollection <CustomAttributeTypedArgument>)attributeArgument.Value;
                    if (args.Count > 0 && args[0].ArgumentType == typeof(byte))
                    {
                        return((byte)args[0].Value == 2);
                    }
                }
                else if (attributeArgument.ArgumentType == typeof(byte))
                {
                    return((byte)attributeArgument.Value == 2);
                }
            }
            var context = member.DeclaringType.CustomAttributes.FirstOrDefault(x => x.AttributeType.FullName == NullableContextAttributeFullName);

            if (context != null && context.ConstructorArguments.Count == 1 && context.ConstructorArguments[0].ArgumentType == typeof(byte))
            {
                return((byte)context.ConstructorArguments[0].Value == 2);
            }
            // Couldn't find a suitable attribute
            return(false);
        }
 internal static bool IsDefined(MemberInfo target, Type caType, bool inherit) {
   // JScript implements subclasses of MemberInfo which throw an exception when Module is
   // accessed. We know that none of these are from a ReflectionOnly assembly.
   Type t = target.GetType();
   if (t.Assembly == typeof(CustomAttribute).Assembly || !target.Module.Assembly.ReflectionOnly)
     return target.IsDefined(caType, inherit);
   return CustomAttribute.CheckForCustomAttribute(CustomAttributeData.GetCustomAttributes(target), caType);
 }
		/// <summary>Checks if the given member is from a class being compiled at the moment.</summary>
		/// <param name="member">The member to check.</param>
		/// <returns>True if the member is from a dynamic type (one being compiled); false otherwise.</returns>
		public static bool IsDynamic(MemberInfo member){
			Type t=member.GetType();
			return (t==typeof(MethodBuilder)||t==typeof(FieldBuilder)||t==typeof(ConstructorBuilder)||t==typeof(TypeBuilder)||t==typeof(LocalBuilder)||t==typeof(PropertyBuilder)||t==typeof(ParameterBuilder));
		}
Beispiel #15
0
        public virtual void RecordFixup(long objectToBeFixed, MemberInfo member, long objectRequired)
        {
            //Verify our arguments
            if (objectToBeFixed <= 0 || objectRequired <= 0)
            {
                throw new ArgumentOutOfRangeException(objectToBeFixed <= 0 ? nameof(objectToBeFixed) : nameof(objectRequired), SR.Serialization_IdTooSmall);
            }
            if (member == null)
            {
                throw new ArgumentNullException(nameof(member));
            }
            if (!(member is FieldInfo))
            {
                throw new SerializationException(SR.Format(SR.Serialization_InvalidType, member.GetType().ToString()));
            }

            //Create a new fixup holder
            FixupHolder fixup = new FixupHolder(objectRequired, member, FixupHolder.MemberFixup);
            RegisterFixup(fixup, objectToBeFixed, objectRequired);
        }
Beispiel #16
0
        // Retrieves the member type from the MemberInfo
        internal Type GetMemberType(MemberInfo objMember)
        {
            if (objMember is FieldInfo)
            {
                return ((FieldInfo)objMember).FieldType;
            }

            throw new SerializationException(SR.Format(SR.Serialization_SerMemberInfo, objMember.GetType()));
        }
 public static Type ReflectedType(this MemberInfo member)
 {
     // this isn't right...
     if (member is MethodInfo)
     {
         return(((MethodInfo)member).ReflectedType());
     }
     else
     {
         throw new NotSupportedException(string.Format("Cannot handle '{0}'.", member.GetType()));
     }
 }
 public static bool IsPublic(this MemberInfo member)
 {
     if (member is MethodBase)
     {
         return(((MethodBase)member).IsPublic);
     }
     else if (member is PropertyInfo)
     {
         PropertyInfo prop = (PropertyInfo)member;
         return((prop.GetMethod != null && prop.GetMethod.IsPublic) || (prop.SetMethod != null && prop.SetMethod.IsPublic));
     }
     else if (member is FieldInfo)
     {
         return(((FieldInfo)member).IsPublic);
     }
     else if (member is EventInfo)
     {
         EventInfo evt = (EventInfo)member;
         return((evt.AddMethod != null && evt.AddMethod.IsPublic) || (evt.RemoveMethod != null && evt.RemoveMethod.IsPublic));
     }
     else
     {
         throw new NotSupportedException(string.Format("Cannot handle '{0}'.", member.GetType()));
     }
 }
Beispiel #19
0
 public static object GetValue(this MemberInfo member, object source)
 {
     return(member switch
     {
         PropertyInfo pi => pi.GetValue(source),
         FieldInfo fi => fi.GetValue(source),
         _ => throw new NotSupportedException(string.Format(PnPCoreResources.Exception_Unsupported_MemberType, member.GetType())),
     });
        private Type GetReturnType(MemberInfo method)
        {
            if (method is MethodInfo)
                return (method as MethodInfo).ReturnType;
            if (method is PropertyInfo)
                return (method as PropertyInfo).PropertyType;
            if (method is FieldInfo)
                return (method as FieldInfo).FieldType;

            throw new InvalidOperationException(String.Format("Method analysis failed for {0}::{1}: could not determine return type for method of type {2}.", method.DeclaringType != null ? method.DeclaringType.Name : "<null>", method.Name, method.GetType().Name));
        }
 virtual public bool TestGetCustomAttributes2 (MemberInfo mem)
   {
   Util.print ("\n\n" + this.GetType ().ToString () + " - TestGetCustomAttributes2() started.");
   Util.print ("For: " + mem.GetType ().ToString () + "\n");
   string strLoc="L_31_000";
   Object[] attrs = null;
   try  
     {
     do
       {
       iCountTestcases++;
       strLoc="L_31_001";
       if (mem == null) {
       iCountErrors++;
       Util.printerr ("E_31_75yhg - mem == null");
       return false;
       }
       iCountTestcases++;
       strLoc="L_31_001.2";
       attrs = mem.GetCustomAttributes (true);
       if (attrs == null) {
       iCountErrors++;
       Util.printerr ("E_31_gfh4 - attrs == null");
       return false;
       }
       iCountTestcases++;
       strLoc="L_31_001.3";
        
            if (attrs.Length != iCountTotalAttrs) {
                            
            iCountErrors++;
            Util.printerr ("E_31_jhd3 - attrs.Length Failed!");
            Util.print ("Expected: " + iCountTotalAttrs + "; Returned: " + attrs.Length);
            Util.print (mem);
            }
       
       iCountTestcases++;
       strLoc="L_31_001.3";
       TestAttributes (attrs, mem);
       } while ( false );
     }
   catch( Exception exc_runTest ) {
   ++iCountErrors;
   Util.printerr ("E_31_888un! - Uncaught Exception caught in TestGetCustomAttributes(); strLoc == " + strLoc);
   Util.printexc (exc_runTest);
   Util.print (exc_runTest.StackTrace);
   }
   if ( iCountErrors == 0 ) {   return true; }
   else {  return false;}
   }
Beispiel #22
0
		/// <summary> Get the value of given member. </summary>
		/// <param name="objectInstance">null if member is static</param>
		public static Value GetMemberValue(Value objectInstance, MemberInfo memberInfo, params Value[] arguments)
		{
			if (memberInfo is FieldInfo) {
				if (arguments.Length > 0)
					throw new GetValueException("Arguments can not be used for a field");
				return GetFieldValue(objectInstance, (FieldInfo)memberInfo);
			} else if (memberInfo is PropertyInfo) {
				return GetPropertyValue(objectInstance, (PropertyInfo)memberInfo, arguments);
			} else if (memberInfo is MethodInfo) {
				return InvokeMethod(objectInstance, (MethodInfo)memberInfo, arguments);
			}
			throw new DebuggerException("Unknown member type: " + memberInfo.GetType());
		}
 override public bool TestGetCustomAttribute_Type (MemberInfo mem)
   {
   Util.print ("\n\n" + this.GetType ().ToString () + " - TestGetCustomAttribute_Type() started.");
   Util.print ("For: " + mem.GetType ().ToString () + "\n");
   string strLoc="L_2_000";
   Attribute attr = null;
   try  
     {
     do
       {
       iCountTestcases++;
       strLoc="L_2_001";
       iCountTestcases++;
       strLoc="L_2_002.2";
       attr = Attribute.GetCustomAttribute (mem, typeof (ClassLib_Attributes.CA_AMfalse_INfalse), true);
       if (attr == null) {
       iCountErrors++;
       Util.printerr ("E_2_gfh4 - attr == null");
       }
       iCountTestcases++;
       strLoc="L_2_001.4";
       if (attr is ClassLib_Attributes.CA_AMfalse_INfalse) {
       if (((ClassLib_Attributes.CA_AMfalse_INfalse) attr).name.Equals ("CA_AMfalse_INfalse2")) {
       iCountTestcases++;
       strLoc="L_2_001.5";
       if (!caAmfInf2.Equals (attr)) {
       iCountErrors++;
       Util.printerr ("E_2_8d2p- caAmfInf2.Equals FAiLed!");
       }
       }
       else {  return false;}
       }
       else {
       iCountErrors++;
       Util.printerr ("E_2_9dus- UnExpected attr type! - " + attr.GetType().ToString ());
       }
       iCountTestcases++;
       strLoc="L_3_002.2";
       attr = Attribute.GetCustomAttribute (mem, typeof (ClassLib_Attributes.CA_AMfalse_INtrue), true);
       if (attr == null) {
       iCountErrors++;
       Util.printerr ("E_3_gfh4 - attr == null");
       }
       iCountTestcases++;
       strLoc="L_3_001.4";
       if (attr is ClassLib_Attributes.CA_AMfalse_INtrue) {
       if (((ClassLib_Attributes.CA_AMfalse_INtrue) attr).name.Equals ("CA_AMfalse_INtrue2")) {
       iCountTestcases++;
       strLoc="L_3_001.5";
       if (!caAmfInt2.Equals (attr)) {
       iCountErrors++;
       Util.printerr ("E_3_8d2p- caAmfInt2.Equals FAiLed!");
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_3_iu2h- UnExpected attr.name! - " + ((ClassLib_Attributes.CA_AMfalse_INtrue) attr).name);
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_3_9dus- UnExpected attr type! - " + attr.GetType().ToString ());
       }
       iCountTestcases++;
       strLoc="L_4_002.2";
       try {
       attr = Attribute.GetCustomAttribute (mem, typeof (ClassLib_Attributes.CA_AMtrue_INtrue), true);
       if (!((mem is FieldInfo) || (mem is ConstructorInfo) || mem is PropertyInfo)) {
       iCountErrors++;
       Util.printerr ("E_4_5_jhd3 - should 've thrown AmbiguousMatchException");
       }
       }
       catch (AmbiguousMatchException ) {
       }
       iCountTestcases++;
       strLoc="L_5_002.2";
       attr = Attribute.GetCustomAttribute (mem, typeof (ClassLib_Attributes.CA_AMtrue_INfalse), true);
       if (attr == null) {
       iCountErrors++;
       Util.printerr ("E_5_gfh4 - attr == null");
       }
       iCountTestcases++;
       strLoc="L_5_001.4";
       if (attr is ClassLib_Attributes.CA_AMtrue_INfalse) {
       if (((ClassLib_Attributes.CA_AMtrue_INfalse) attr).name.Equals ("CA_AMtrue_INfalse3")) {
       iCountTestcases++;
       strLoc="L_5_001.5";
       if (!caAmtInf3.Equals (attr)) {
       iCountErrors++;
       Util.printerr ("E_5_8d2p5- caAmtInf3.Equals FAiLed!");
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_5_djfh - UnExpected attr name! - " + ((ClassLib_Attributes.CA_AMtrue_INfalse) attr).name);
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_5_9dus- UnExpected attr type! - " + attr.GetType().ToString ());
       }
       iCountTestcases++;
       strLoc="L_6_002.2";
       try {
       attr = Attribute.GetCustomAttribute (mem, typeof (System.Attribute), true);
       iCountErrors++;
       Util.printerr ("E_6_5_jhd3 - should 've thrown AmbiguousMatchException");
       }
       catch (AmbiguousMatchException ) {
       }
       } while ( false );
     }
   catch( Exception exc_runTest ) {
   ++iCountErrors;
   Util.printerr ("Err_333un! - Uncaught Exception caught in TestGetCustomAttribute_Type(); strLoc == " + strLoc);
   Util.printexc (exc_runTest);
   Util.print (exc_runTest.StackTrace);
   }
   if ( iCountErrors == 0 ) {   return true; }
   else {
   return false;
   }
   }
 virtual public bool TestGetCustomAttribute_Type2 (MemberInfo mem)
   {
   Util.print ("\n\n" + this.GetType ().ToString () + " - TestGetCustomAttribute_Type2() started.");
   Util.print ("For: " + mem.GetType ().ToString () + "\n");
   string strLoc="L_111_2_000";
   String str = null;
   Object[] attrs = null;
   try  
     {
     do
       {
       iCountTestcases++;
       strLoc="L_111_2_001";
       iCountTestcases++;
       strLoc="L_111_2_002.2";
       attrs = mem.GetCustomAttributes (typeof (ClassLib_Attributes.CA_AMfalse_INfalse), true);
       if (attrs == null) {
       iCountErrors++;
       Util.printerr ("E_111_2_gfh4 - attrs == null");
       }
       iCountTestcases++;
       strLoc="L_111_2_002.3";
       if (attrs.Length != iCountAMfalseINfalseAttrs) {
       iCountErrors++;
       Util.printerr ("E_111_2_jhd3 - attrs.Length != iCountAMfalseINfalseAttrs");
       Util.print ("Expected: " + iCountAMfalseINfalseAttrs + ";Returned: " + attrs.Length);
       }
       iCountTestcases++;
       strLoc="L_111_2_002.3.2";
       if (!(attrs is ClassLib_Attributes.CA_AMfalse_INfalse[])) {
       iCountErrors++;
       Util.printerr ("E_111_2_jhd32 - attrs is ClassLib_Attributes.CA_AMfalse_INfalse[] FAiLed!");
       }
       if (Util.DEBUG > 0) Console.WriteLine("Number of attrs {0}", attrs.Length);
       for (int i = 0; i < attrs.Length; i++) {
       iCountTestcases++;
       strLoc="L_111_2_001.3.2";
       if (Util.DEBUG > 1) Console.WriteLine ("{0} : {1}\n\n", i, attrs[i]);
       str = attrs[i].ToString ();
       iCountTestcases++;
       strLoc="L_111_2_001.4";
       if (attrs[i] is ClassLib_Attributes.CA_AMfalse_INfalse) {
       ClassLib_Attributes.CA_AMfalse_INfalse attr = (ClassLib_Attributes.CA_AMfalse_INfalse) attrs[i];
       if (attr.name.Equals ("CA_AMfalse_INfalse")) {
       iCountTestcases++;
       strLoc="L_111_2_001.5";
       if (!caAmfInf.Equals (attr)) {
       iCountErrors++;
       Util.printerr ("E_111_2_8d2p- caAmfInf2.Equals FAiLed!");
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_111_2_iu2h- UnExpected attr.name! - " + attr.name);
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_111_2_9dus- UnExpected attr type! - " + attrs[i].GetType().ToString ());
       }
       }
       iCountTestcases++;
       strLoc="L_111_3_002.2";
       attrs = mem.GetCustomAttributes (typeof (ClassLib_Attributes.CA_AMfalse_INtrue), true);
       if (attrs == null) {
       iCountErrors++;
       Util.printerr ("E_111_3_gfh4 - attrs == null");
       }
       iCountTestcases++;
       strLoc="L_111_3_002.3";
       if (attrs.Length != iCountAMfalseINtrueAttrs) {
       iCountErrors++;
       Util.printerr ("E_111_3_jhd3 - attrs.Length != iCountAMfalseINtrueAttrs");
       Util.print ("Expected: " + iCountAMfalseINtrueAttrs + ";Returned: " + attrs.Length);
       }
       iCountTestcases++;
       strLoc="L_111_3_002.3.2";
       if (!(attrs is ClassLib_Attributes.CA_AMfalse_INtrue[])) {
       iCountErrors++;
       Util.printerr ("E_111_3_jhd32 - attrs is ClassLib_Attributes.CA_AMfalse_INtrue[] FAiLed!");
       }
       if (Util.DEBUG > 0) Console.WriteLine("Number of attrs {0}", attrs.Length);
       for (int i = 0; i < attrs.Length; i++) {
       iCountTestcases++;
       strLoc="L_111_3_001.3.2";
       if (Util.DEBUG > 1) Console.WriteLine ("{0} : {1}\n\n", i, attrs[i]);
       str = attrs[i].ToString ();
       iCountTestcases++;
       strLoc="L_111_3_001.4";
       if (attrs[i] is ClassLib_Attributes.CA_AMfalse_INtrue) {
       ClassLib_Attributes.CA_AMfalse_INtrue attr = (ClassLib_Attributes.CA_AMfalse_INtrue) attrs[i];
       if (attr.name.Equals ("CA_AMfalse_INtrue")) {
       iCountTestcases++;
       strLoc="L_111_3_001.5";
       if (!caAmfInt.Equals (attr)) {
       iCountErrors++;
       Util.printerr ("E_111_3_8d2p- caAmfInt2.Equals FAiLed!");
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_111_3_iu2h- UnExpected attr.name! - " + attr.name);
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_111_3_9dus- UnExpected attr type! - " + attrs[i].GetType().ToString ());
       }
       }
       iCountTestcases++;
       strLoc="L_111_4_002.2";
       attrs = mem.GetCustomAttributes (typeof (ClassLib_Attributes.CA_AMtrue_INtrue), true);
       if (attrs == null) {
       iCountErrors++;
       Util.printerr ("E_111_4_gfh4 - attrs == null");
       }
       iCountTestcases++;
       strLoc="L_111_4_002.3";
       if (attrs.Length != iCountAMtrueINtrueAttrs) {
       iCountErrors++;
       Util.printerr ("E_111_4_jhd3 - attrs.Length != iCountAMtrueINtrueAttrs");
       Util.print ("Expected: " + iCountAMtrueINtrueAttrs + ";Returned: " + attrs.Length);
       }
       iCountTestcases++;
       strLoc="L_111_4_002.3.2";
       if (!(attrs is ClassLib_Attributes.CA_AMtrue_INtrue[])) {
       iCountErrors++;
       Util.printerr ("E_111_4_jhd32 - attrs is ClassLib_Attributes.CA_AMtrue_INtrue[] FAiLed!");
       }
       if (Util.DEBUG > 0) Console.WriteLine("Number of attrs {0}", attrs.Length);
       for (int i = 0; i < attrs.Length; i++) {
       iCountTestcases++;
       strLoc="L_111_4_001.3.2";
       if (Util.DEBUG > 1) Console.WriteLine ("{0} : {1}\n\n", i, attrs[i]);
       str = attrs[i].ToString ();
       iCountTestcases++;
       strLoc="L_111_4_001.4";
       if (attrs[i] is ClassLib_Attributes.CA_AMtrue_INtrue) {
       ClassLib_Attributes.CA_AMtrue_INtrue attr = (ClassLib_Attributes.CA_AMtrue_INtrue) attrs[i];
       if (attr.name.Equals ("CA_AMtrue_INtrue")) {
       iCountTestcases++;
       strLoc="L_111_4_001.5";
       if (!caAmtInt.Equals (attr)) {
       iCountErrors++;
       Util.printerr ("E_111_4_8d2p5- caAmtInt.Equals FAiLed!");
       }
       }
       else if (attr.name.Equals ("CA_AMtrue_INtrue2")) {
       iCountTestcases++;
       strLoc="L_111_4_001.6";
       if (!caAmtInt2.Equals (attr)) {
       iCountErrors++;
       Util.printerr ("E_111_4_8d2p6- caAmtInt2.Equals FAiLed!");
       }
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_111_4_9dus- UnExpected attr type! - " + attrs[i].GetType().ToString ());
       }
       }
       iCountTestcases++;
       strLoc="L_111_5_002.2";
       attrs = mem.GetCustomAttributes (typeof (ClassLib_Attributes.CA_AMtrue_INfalse), true);
       if (attrs == null) {
       iCountErrors++;
       Util.printerr ("E_111_5_gfh4 - attrs == null");
       }
       iCountTestcases++;
       strLoc="L_111_5_002.3";
       if (attrs.Length != iCountAMtrueINfalseAttrs) {
       iCountErrors++;
       Util.printerr ("E_111_5_jhd3 - attrs.Length != iCountAMtrueINfalseAttrs");
       Util.print ("Expected: " + iCountAMtrueINfalseAttrs + ";Returned: " + attrs.Length);
       }
       iCountTestcases++;
       strLoc="L_111_5_002.3.2";
       if (!(attrs is ClassLib_Attributes.CA_AMtrue_INfalse[])) {
       iCountErrors++;
       Util.printerr ("E_111_5_jhd32 - attrs is ClassLib_Attributes.CA_AMtrue_INfalse[] FAiLed!");
       }
       if (Util.DEBUG > 0) Console.WriteLine("Number of attrs {0}", attrs.Length);
       for (int i = 0; i < attrs.Length; i++) {
       iCountTestcases++;
       strLoc="L_111_5_001.3.2";
       if (Util.DEBUG > 1) Console.WriteLine ("{0} : {1}\n\n", i, attrs[i]);
       str = attrs[i].ToString ();
       iCountTestcases++;
       strLoc="L_111_5_001.4";
       if (attrs[i] is ClassLib_Attributes.CA_AMtrue_INfalse) {
       ClassLib_Attributes.CA_AMtrue_INfalse attr = (ClassLib_Attributes.CA_AMtrue_INfalse) attrs[i];
       if (attr.name.Equals ("CA_AMtrue_INfalse")) {
       iCountTestcases++;
       strLoc="L_111_5_001.5";
       if (!caAmtInf.Equals (attr)) {
       iCountErrors++;
       Util.printerr ("E_111_5_8d2p5- caAmtInf.Equals FAiLed!");
       }
       }
       else if (attr.name.Equals ("CA_AMtrue_INfalse2")) {
       iCountTestcases++;
       strLoc="L_111_5_001.5";
       if (!caAmtInf2.Equals (attr)) {
       iCountErrors++;
       Util.printerr ("E_111_5_8d2p5- caAmtInf2.Equals FAiLed!");
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_111_5_djfh - UnExpected attr name! - " + attr.name);
       }
       }
       else {
       iCountErrors++;
       Util.printerr ("E_111_5_9dus- UnExpected attr type! - " + attrs[i].GetType().ToString ());
       }
       }
       iCountTestcases++;
       strLoc="L_111_6_002.2";
       attrs = mem.GetCustomAttributes (typeof (System.Attribute), true);
       if (attrs == null) {
       iCountErrors++;
       Util.printerr ("E_111_6_gfh4 - attrs == null");
       }
       iCountTestcases++;
       strLoc="L_111_6_002.3";
       if (attrs.Length != iCountTotalAttrs) {
       iCountErrors++;
       Util.printerr ("E_111_6_jhd3 - attrs.Length != iCountTotalAttrs");
       Util.print ("Expected: " + iCountTotalAttrs + ";Returned: " + attrs.Length);
       }
       iCountTestcases++;
       strLoc="L_111_6_002.4";
       TestAttributes (attrs, mem);
       } while ( false );
     }
   catch( Exception exc_runTest ) {
   ++iCountErrors;
   Util.printerr ("E_111_333un! - Uncaught Exception caught in TestGetCustomAttribute_Type(); strLoc == " + strLoc);
   Util.printexc (exc_runTest);
   Util.print (exc_runTest.StackTrace);
   }
   if ( iCountErrors == 0 ) {   return true; }
   else {
   return false;
   }
   }
Beispiel #25
0
 static char GetMemberPrefix(MemberInfo member)
 {
     return member.GetType().Name
       .Replace("Runtime", "")[0];
 }
 internal static string ToSignature(MemberInfo mi)
 {
     if (mi is PropertyInfo)
         return ToSignature ((PropertyInfo)mi);
     if (mi is FieldInfo)
         return ToSignature ((FieldInfo)mi);
     if (mi is ConstructorInfo)
         return ToSignature ((ConstructorInfo)mi);
     if (mi is MethodInfo)
         return ToSignature ((MethodInfo)mi);
     return "[unknown: " + mi.GetType ().FullName + "]";
 }
 public static MemberTypes MemberType(this MemberInfo member)
 {
     if (member is MethodInfo)
     {
         return(((MethodInfo)member).MemberType());
     }
     else
     {
         throw new NotSupportedException(string.Format("Cannot handle '{0}'.", member.GetType()));
     }
 }
        // ashmind: this method is a bit too hardcoded, on the other hand it does not make
        // sense to ask IPropertyAccessor to find accessor by name when we already have MemberInfo
        private IGetter GetGetterFast(Type type, MemberInfo member)
        {
            if (member is PropertyInfo)
                return new BasicPropertyAccessor.BasicGetter(type, (PropertyInfo)member, member.Name);

            if (member is FieldInfo)
                return new FieldAccessor.FieldGetter((FieldInfo)member, type, member.Name);

            throw new ArgumentException("Can not get getter for " + member.GetType() + ".", "member");
        }
 override public bool TestGetCustomAttributes2 (MemberInfo mem)
   {
   Util.print ("\n\n" + this.GetType ().ToString () + " - TestGetCustomAttributes2() started.");
   Util.print ("For: " + mem.GetType ().ToString () + "\n");
   string strLoc="L_16_000";
   Object[] attrs = null;
   try  
     {
     do
       {
       iCountTestcases++;
       strLoc="L_16_001";
       if (mem == null) {
       iCountErrors++;
       Util.printerr ("E_75yhg - mem == null");
       return false;
       }
       iCountTestcases++;
       strLoc="L_16_001.1.1";
       attrs = mem.GetCustomAttributes (false);
       if (attrs == null) {
       iCountErrors++;
       Util.printerr ("E_16_dkd3 - attrs == null");
       return false;
       }
        
       if (attrs.Length != (iCountTotalAttrs-2)) {
       iCountErrors++;
       Util.printerr ("E_16_499s - attrs.Length != iCountTotalAttrs-2");
       Util.print ("Expected: " + (iCountTotalAttrs-2) + ";Returned: " + attrs.Length);
       Util.print (mem);
       
       }
       iCountTestcases++;
       strLoc="L_16_001.2";
       attrs = mem.GetCustomAttributes (true);
       if (attrs == null) {
       iCountErrors++;
       Util.printerr ("E_gfh4 - attrs == null");
       return false;
       }
       iCountTestcases++;
       strLoc="L_16_001.3";
       if ((mem is System.Reflection.FieldInfo) || (mem is System.Reflection.ConstructorInfo)
       || (mem is System.Reflection.PropertyInfo) || (mem is System.Reflection.EventInfo)) { 
            if (attrs.Length != (iCountTotalAttrs-2)) {
                iCountErrors++;
                Util.printerr ("E_16_fk25 - attrs.Length != iCountTotalAttrs-2");
                Util.print ("Expected: " + (iCountTotalAttrs-2) + ";Returned: " + attrs.Length);                    
        }
        }
       
        else
        {
         
        if (attrs.Length != iCountTotalAttrs) {
            iCountErrors++;
            Util.printerr ("E_16_jhd4 - attrs.Length != iCountTotalAttrs");
            Util.print ("Expected: " + iCountTotalAttrs + "; Returned: " + attrs.Length);
        
        }
       }
       iCountTestcases++;
       strLoc="L_16_001.3";
       TestAttributes (attrs, mem);
       } while ( false );
     }
   catch( Exception exc_runTest ) {
   ++iCountErrors;
   Util.printerr ("E_16_888un! - Uncaught Exception caught in TestGetCustomAttributes(); strLoc == " + strLoc);
   Util.printexc (exc_runTest);
   Util.print (exc_runTest.StackTrace);
   }
   if ( iCountErrors == 0 ) {   return true; }
   else {
   return false;
   }
   }
 private static MemberSetter ThrowGetOnlyMemberIsInvalid(MemberInfo member)
 {
     var asProperty = member as PropertyInfo;
     if (asProperty != null)
     {
         throw new SerializationException(String.Format(CultureInfo.CurrentCulture, "Cannot set value to '{0}.{1}' property.", asProperty.DeclaringType, asProperty.Name));
     }
     else
     {
         Contract.Assert(member is FieldInfo, member.ToString() + ":" + member.GetType());
         throw new SerializationException(
             String.Format(
                 CultureInfo.CurrentCulture, "Cannot set value to '{0}.{1}' field.", member.DeclaringType, member.Name
             )
         );
     }
 }
        public string GenerateMethodTest(MemberInfo mi)
        {
            string result = "";
            if (TypeToTest.IsSubclassOf(mi.ReflectedType))
                return result;
            object[] classAttributes = TypeToTest.GetCustomAttributes(true);
            IEnumerable<object> preConditions = classAttributes.Where(attr => attr.GetType() == typeof(PreCondition));
            PreCondition preCondition = preConditions.Select(pc => (PreCondition)pc).Where(pre => pre.ConstructorVariable != null).First();

            int index = 0;
            result += GenerateTestMethodHeader(mi, index);
            result += GeneratePreConditions(mi);
            if (mi.GetType() == typeof(ConstructorInfo) || mi.GetType().BaseType == typeof(ConstructorInfo))
                result += GenerateConstructorCall((ConstructorInfo)mi, preCondition.ConstructorVariable) + "\n";
            else
                result += GenerateMethodCall((MethodInfo)mi, preCondition.ConstructorVariable) + "\n";
            result += GeneratePostConditions(mi);
            result += GenerateTestMethodFooter();
            return result;
        }
        // Retrieves the member type from the MemberInfo
        internal  Type GetMemberType(MemberInfo objMember)
        {
            Type objectType = null;

            if (objMember is FieldInfo)
            {
                objectType = ((FieldInfo)objMember).FieldType;
                SerTrace.Log( this, objectInfoId," ", "GetMemberType FieldInfo ",objectType);
            }
            else if (objMember is PropertyInfo)
            {
                objectType = ((PropertyInfo)objMember).PropertyType;
                SerTrace.Log( this,objectInfoId," ", "GetMemberType PropertyInfo ",objectType);
            }
            else
            {
                throw new SerializationException(Environment.GetResourceString("Serialization_SerMemberInfo",objMember.GetType()));
            }

            return objectType;
        }
 internal static object[] GetCustomAttributes(Type attributeType, bool inherit, Attribute[] attributes, MemberInfo memberInfo)
 {
     if (attributeType == null)
     {
         throw new ArgumentNullException("attributeType");
     }
     ArrayList list = new ArrayList();
     ArrayList list2 = new ArrayList();
     if (attributeType == typeof(object))
     {
         list.AddRange(attributes);
     }
     else
     {
         foreach (AttributeInfoAttribute attribute in attributes)
         {
             if (attribute.AttributeInfo.AttributeType == attributeType)
             {
                 list.Add(attribute);
                 list2.Add(attributeType);
             }
         }
     }
     if (inherit)
     {
         MemberInfo baseType = null;
         if (memberInfo is Type)
         {
             baseType = ((Type) memberInfo).BaseType;
         }
         else
         {
             baseType = ((DesignTimeType) memberInfo.DeclaringType).GetBaseMember(memberInfo.GetType(), memberInfo.DeclaringType.BaseType, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance, new DesignTimeType.MemberSignature(memberInfo));
         }
         if (baseType != null)
         {
             foreach (Attribute attribute2 in baseType.GetCustomAttributes(attributeType, inherit))
             {
                 if (!(attribute2 is AttributeInfoAttribute) || !list2.Contains(((AttributeInfoAttribute) attribute2).AttributeInfo.AttributeType))
                 {
                     list.Add(attribute2);
                 }
             }
         }
     }
     return list.ToArray();
 }
Beispiel #34
0
        /// <include file='doc\ObjectManager.uex' path='docs/doc[@for="ObjectManager.RecordFixup"]/*' />
        public virtual void RecordFixup(long objectToBeFixed, MemberInfo member, long objectRequired) {
    
            //Verify our arguments
            if (objectToBeFixed<=0 || objectRequired<=0) {
                throw new ArgumentOutOfRangeException(((objectToBeFixed<=0)?"objectToBeFixed":"objectRequired"),
                                                      Environment.GetResourceString("Serialization_IdTooSmall"));
            }

            if (member==null) {
                throw new ArgumentNullException("member");
            }

            if (!(member is RuntimeFieldInfo) && !(member is SerializationFieldInfo)) {
                throw new SerializationException(String.Format(Environment.GetResourceString("Serialization_InvalidType"), member.GetType().ToString()));
            }

    
            BCLDebug.Trace("SER", "RecordFixup.  ObjectToBeFixed: ", objectToBeFixed, "\tMember: ", member.Name, "\tRequiredObject: ", objectRequired);
    
            //Create a new fixup holder
            FixupHolder fixup = new FixupHolder(objectRequired, member, FixupHolder.MemberFixup);
    
            RegisterFixup(fixup, objectToBeFixed, objectRequired);
        }
 internal static bool IsDefined(Type attributeType, bool inherit, Attribute[] attributes, MemberInfo memberInfo)
 {
     if (attributeType == null)
     {
         throw new ArgumentNullException("attributeType");
     }
     foreach (Attribute attribute in attributes)
     {
         if ((attribute is AttributeInfoAttribute) && ((attribute as AttributeInfoAttribute).AttributeInfo.AttributeType == attributeType))
         {
             return true;
         }
     }
     MemberInfo baseType = null;
     if (memberInfo is Type)
     {
         baseType = ((Type) memberInfo).BaseType;
     }
     else
     {
         baseType = ((DesignTimeType) memberInfo.DeclaringType).GetBaseMember(memberInfo.GetType(), memberInfo.DeclaringType.BaseType, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance, new DesignTimeType.MemberSignature(memberInfo));
     }
     return ((baseType != null) && baseType.IsDefined(attributeType, inherit));
 }