Mono.GetOptions.OptionDetails.TypeOfMember C# (CSharp) Метод

TypeOfMember() приватный статический Метод

private static TypeOfMember ( MemberInfo memberInfo ) : Type
memberInfo System.Reflection.MemberInfo
Результат System.Type
		private static Type TypeOfMember(MemberInfo memberInfo)
		{
			if ((memberInfo.MemberType == MemberTypes.Field && memberInfo is FieldInfo))
				return ((FieldInfo) memberInfo).FieldType;

			if ((memberInfo.MemberType == MemberTypes.Property && memberInfo is PropertyInfo))
				return ((PropertyInfo) memberInfo).PropertyType;

			if ((memberInfo.MemberType == MemberTypes.Method && memberInfo is MethodInfo))
			{
				if (((MethodInfo) memberInfo).ReturnType.FullName != typeof(WhatToDoNext).FullName)
					throw new NotSupportedException("Option method must return '" + typeof(WhatToDoNext).FullName + "'");

				ParameterInfo[] parameters = ((MethodInfo) memberInfo).GetParameters();
				if ((parameters == null) || (parameters.Length == 0))
					return null;
				else
					return parameters[0].ParameterType;
			}

			throw new NotSupportedException("'" + memberInfo.MemberType + "' memberType is not supported");
		}

Usage Example

Пример #1
0
 public OptionDetails(System.Reflection.MemberInfo memberInfo, OptionAttribute option, Options optionBundle)
 {
     this.paramName  = null;
     this.optionHelp = null;
     this.ShortForm  = ("" + option.ShortForm).Trim();
     if (option.LongForm == null)
     {
         this.LongForm = string.Empty;
     }
     else
     {
         this.LongForm = (option.LongForm != string.Empty) ? option.LongForm : memberInfo.Name;
     }
     this.AlternateForm    = option.AlternateForm;
     this.ShortDescription = this.ExtractParamName(option.ShortDescription);
     this.Occurs           = 0;
     this.OptionBundle     = optionBundle;
     this.BooleanOption    = false;
     this.MemberInfo       = memberInfo;
     this.NeedsParameter   = false;
     this.Values           = null;
     this.MaxOccurs        = 1;
     this.VBCStyleBoolean  = option.VBCStyleBoolean;
     this.SecondLevelHelp  = option.SecondLevelHelp;
     this.ParameterType    = OptionDetails.TypeOfMember(memberInfo);
     if (this.ParameterType != null)
     {
         if (this.ParameterType.FullName != "System.Boolean")
         {
             if (this.LongForm.IndexOf(':') >= 0)
             {
                 throw new InvalidOperationException("Options with an embedded colon (':') in their visible name must be boolean!!! [" + this.MemberInfo.ToString() + " isn't]");
             }
             this.NeedsParameter = true;
             if (option.MaxOccurs == 1)
             {
                 return;
             }
             if (this.ParameterType.IsArray)
             {
                 this.Values    = new ArrayList();
                 this.MaxOccurs = option.MaxOccurs;
                 return;
             }
             if ((this.MemberInfo is MethodInfo) || (this.MemberInfo is PropertyInfo))
             {
                 this.MaxOccurs = option.MaxOccurs;
                 return;
             }
             object[] objArray1 = new object[] { "MaxOccurs set to non default value (", option.MaxOccurs, ") for a [", this.MemberInfo.ToString(), "] option" };
             throw new InvalidOperationException(string.Concat(objArray1));
         }
         this.BooleanOption = true;
         if (option.MaxOccurs != 1)
         {
             if ((this.MemberInfo is MethodInfo) || (this.MemberInfo is PropertyInfo))
             {
                 this.MaxOccurs = option.MaxOccurs;
             }
             else
             {
                 object[] objArray2 = new object[] { "MaxOccurs set to non default value (", option.MaxOccurs, ") for a [", this.MemberInfo.ToString(), "] option" };
                 throw new InvalidOperationException(string.Concat(objArray2));
             }
         }
     }
 }