Exemplo n.º 1
0
		void SetCustomAttribute (CodeMemberMethod method, UnknownAttributeDescriptor uad)
		{
			CodeAssignStatement assign = new CodeAssignStatement ();
			assign.Left = new CodePropertyReferenceExpression (
				new CodeArgumentReferenceExpression("__ctrl"),
				uad.Info.Name);
			assign.Right = GetExpressionFromString (uad.Value.GetType (), uad.Value.ToString (), uad.Info);
			
			method.Statements.Add (assign);
		}
Exemplo n.º 2
0
        void CheckUnknownAttribute(string name, string val, string inherits)
        {
            MemberInfo mi         = null;
            bool       missing    = false;
            string     memberName = name.Trim().ToLower(Helpers.InvariantCulture);
            Type       parent     = codeFileBaseClassType;

            if (parent == null)
            {
                parent = baseType;
            }

            try {
                MemberInfo[] infos = parent.GetMember(memberName,
                                                      MemberTypes.Field | MemberTypes.Property,
                                                      BindingFlags.Public | BindingFlags.Instance |
                                                      BindingFlags.IgnoreCase | BindingFlags.Static);
                if (infos.Length != 0)
                {
                    // prefer public properties to public methods (it's what MS.NET does)
                    foreach (MemberInfo tmp in infos)
                    {
                        if (tmp is PropertyInfo)
                        {
                            mi = tmp;
                            break;
                        }
                    }
                    if (mi == null)
                    {
                        mi = infos [0];
                    }
                }
                else
                {
                    missing = true;
                }
            } catch (Exception) {
                missing = true;
            }
            if (missing)
            {
                ThrowParseException(
                    "Error parsing attribute '{0}': Type '{1}' does not have a public property named '{0}'",
                    memberName, inherits);
            }

            Type memberType = null;

            if (mi is PropertyInfo)
            {
                PropertyInfo pi = mi as PropertyInfo;

                if (!pi.CanWrite)
                {
                    ThrowParseException(
                        "Error parsing attribute '{0}': The '{0}' property is read-only and cannot be set.",
                        memberName);
                }
                memberType = pi.PropertyType;
            }
            else if (mi is FieldInfo)
            {
                memberType = ((FieldInfo)mi).FieldType;
            }
            else
            {
                ThrowParseException("Could not determine member the kind of '{0}' in base type '{1}",
                                    memberName, inherits);
            }
            TypeConverter converter   = TypeDescriptor.GetConverter(memberType);
            bool          convertible = true;
            object        value       = null;

            if (converter == null || !converter.CanConvertFrom(typeof(string)))
            {
                convertible = false;
            }

            if (convertible)
            {
                try {
                    value = converter.ConvertFromInvariantString(val);
                } catch (Exception) {
                    convertible = false;
                }
            }

            if (!convertible)
            {
                ThrowParseException("Error parsing attribute '{0}': Cannot create an object of type '{1}' from its string representation '{2}' for the '{3}' property.",
                                    memberName, memberType, val, mi.Name);
            }

            UnknownAttributeDescriptor desc = new UnknownAttributeDescriptor(mi, value);

            unknownMainAttributes.Add(desc);
        }
Exemplo n.º 3
0
		void CheckUnknownAttribute (string name, string val, string inherits)
		{
			MemberInfo mi = null;
			bool missing = false;
			string memberName = name.Trim ().ToLower (Helpers.InvariantCulture);
			Type parent = codeFileBaseClassType;

			if (parent == null)
				parent = baseType;
			
			try {
				MemberInfo[] infos = parent.GetMember (memberName,
								       MemberTypes.Field | MemberTypes.Property,
								       BindingFlags.Public | BindingFlags.Instance |
								       BindingFlags.IgnoreCase | BindingFlags.Static);
				if (infos.Length != 0) {
					// prefer public properties to public methods (it's what MS.NET does)
					foreach (MemberInfo tmp in infos) {
						if (tmp is PropertyInfo) {
							mi = tmp;
							break;
						}
					}
					if (mi == null)
						mi = infos [0];
				} else
					missing = true;
			} catch (Exception) {
				missing = true;
			}
			if (missing)
				ThrowParseException (
					"Error parsing attribute '{0}': Type '{1}' does not have a public property named '{0}'",
					memberName, inherits);
			
			Type memberType = null;
			if (mi is PropertyInfo) {
				PropertyInfo pi = mi as PropertyInfo;
				
				if (!pi.CanWrite)
					ThrowParseException (
						"Error parsing attribute '{0}': The '{0}' property is read-only and cannot be set.",
						memberName);
				memberType = pi.PropertyType;
			} else if (mi is FieldInfo) {
				memberType = ((FieldInfo)mi).FieldType;
			} else
				ThrowParseException ("Could not determine member the kind of '{0}' in base type '{1}",
						     memberName, inherits);
			TypeConverter converter = TypeDescriptor.GetConverter (memberType);
			bool convertible = true;
			object value = null;
			
			if (converter == null || !converter.CanConvertFrom (typeof (string)))
				convertible = false;

			if (convertible) {
				try {
					value = converter.ConvertFromInvariantString (val);
				} catch (Exception) {
					convertible = false;
				}
			}

			if (!convertible)
				ThrowParseException ("Error parsing attribute '{0}': Cannot create an object of type '{1}' from its string representation '{2}' for the '{3}' property.",
						     memberName, memberType, val, mi.Name);
			
			UnknownAttributeDescriptor desc = new UnknownAttributeDescriptor (mi, value);
			unknownMainAttributes.Add (desc);
		}