static IReturnType Parse(IProjectContent pc, IEnumerator <string> tokenizer) { string typeName = tokenizer.Current; if (typeName == null) { throw new ReflectionTypeNameSyntaxError("Unexpected end of type name"); } tokenizer.MoveNext(); int typeParameterCount; typeName = ReflectionClass.SplitTypeParameterCountFromReflectionName(typeName, out typeParameterCount); IReturnType rt = new GetClassReturnType(pc, typeName, typeParameterCount); if (tokenizer.Current == "[") { // this is a constructed type List <IReturnType> typeArguments = new List <IReturnType>(); do { tokenizer.MoveNext(); if (tokenizer.Current != "[") { throw new ReflectionTypeNameSyntaxError("Expected '['"); } tokenizer.MoveNext(); typeArguments.Add(Parse(pc, tokenizer)); if (tokenizer.Current != "]") { throw new ReflectionTypeNameSyntaxError("Expected ']' after generic argument"); } tokenizer.MoveNext(); } while (tokenizer.Current == ","); if (tokenizer.Current != "]") { throw new ReflectionTypeNameSyntaxError("Expected ']' after generic argument list"); } tokenizer.MoveNext(); rt = new ConstructedReturnType(rt, typeArguments); } while (tokenizer.Current == ",") { tokenizer.MoveNext(); string token = tokenizer.Current; if (token != null && token != "," && token != "[" && token != "]") { tokenizer.MoveNext(); } } return(rt); }
public ReflectionMethod(MethodBase methodBase, ReflectionClass declaringType) : base(declaringType, methodBase is ConstructorInfo ? "#ctor" : methodBase.Name) { if (methodBase is MethodInfo) { MethodInfo m = ((MethodInfo)methodBase); this.ReturnType = ReflectionReturnType.Create(this, m.ReturnType, attributeProvider: m.ReturnTypeCustomAttributes); } else if (methodBase is ConstructorInfo) { this.ReturnType = DeclaringType.DefaultReturnType; } foreach (ParameterInfo paramInfo in methodBase.GetParameters()) { this.Parameters.Add(new ReflectionParameter(paramInfo, this)); } if (methodBase.IsGenericMethodDefinition) { foreach (Type g in methodBase.GetGenericArguments()) { this.TypeParameters.Add(new DefaultTypeParameter(this, g)); } int i = 0; foreach (Type g in methodBase.GetGenericArguments()) { ReflectionClass.AddConstraintsFromType(this.TypeParameters[i++], g); } } ModifierEnum modifiers = ModifierEnum.None; if (methodBase.IsStatic) { modifiers |= ModifierEnum.Static; } if (methodBase.IsPrivate) // I assume that private is used most and public last (at least should be) { modifiers |= ModifierEnum.Private; } else if (methodBase.IsFamily || methodBase.IsFamilyOrAssembly) { modifiers |= ModifierEnum.Protected; } else if (methodBase.IsPublic) { modifiers |= ModifierEnum.Public; } else { modifiers |= ModifierEnum.Internal; } if (methodBase.IsFinal) { modifiers |= ModifierEnum.Sealed; } else if (methodBase.IsAbstract) { modifiers |= ModifierEnum.Abstract; } else if (methodBase.IsVirtual) { if ((methodBase.Attributes & MethodAttributes.NewSlot) != 0) { modifiers |= ModifierEnum.Virtual; } else { modifiers |= ModifierEnum.Override; } } this.Modifiers = modifiers; ReflectionClass.AddAttributes(declaringType.ProjectContent, this.Attributes, CustomAttributeData.GetCustomAttributes(methodBase)); ApplySpecialsFromAttributes(this); }
/// <summary> /// Creates a IReturnType from the reflection type. /// </summary> /// <param name="pc">The project content used as context.</param> /// <param name="member">The member used as context (e.g. as GenericReturnType)</param> /// <param name="type">The reflection return type that should be converted</param> /// <param name="createLazyReturnType">Set this parameter to false to create a direct return type /// (without GetClassReturnType indirection) where possible</param> /// <param name="forceGenericType">Set this parameter to false to allow unbound generic types</param> /// <returns>The IReturnType</returns> public static IReturnType Create(IProjectContent pc, IEntity member, Type type, bool createLazyReturnType, bool forceGenericType) { if (type.IsByRef) { // TODO: Use ByRefRefReturnType return(Create(pc, member, type.GetElementType(), createLazyReturnType)); } else if (type.IsPointer) { return(new PointerReturnType(Create(pc, member, type.GetElementType(), createLazyReturnType))); } else if (type.IsArray) { return(new ArrayReturnType(pc, Create(pc, member, type.GetElementType(), createLazyReturnType), type.GetArrayRank())); } else if (type.IsGenericType && (forceGenericType || !type.IsGenericTypeDefinition)) { Type[] args = type.GetGenericArguments(); List <IReturnType> para = new List <IReturnType>(args.Length); for (int i = 0; i < args.Length; ++i) { para.Add(Create(pc, member, args[i], createLazyReturnType)); } return(new ConstructedReturnType(Create(pc, member, type.GetGenericTypeDefinition(), createLazyReturnType, false), para)); } else if (type.IsGenericParameter) { IClass c = (member is IClass) ? (IClass)member : (member is IMember) ? ((IMember)member).DeclaringType : null; if (c != null && type.GenericParameterPosition < c.TypeParameters.Count) { if (c.TypeParameters[type.GenericParameterPosition].Name == type.Name) { return(new GenericReturnType(c.TypeParameters[type.GenericParameterPosition])); } } if (type.DeclaringMethod != null) { IMethod method = member as IMethod; if (method != null) { if (type.GenericParameterPosition < method.TypeParameters.Count) { return(new GenericReturnType(method.TypeParameters[type.GenericParameterPosition])); } return(new GenericReturnType(new DefaultTypeParameter(method, type))); } } return(new GenericReturnType(new DefaultTypeParameter(c, type))); } else { string name = type.FullName; if (name == null) { throw new ApplicationException("type.FullName returned null. Type: " + type.ToString()); } int typeParameterCount; name = ReflectionClass.ConvertReflectionNameToFullName(name, out typeParameterCount); if (!createLazyReturnType) { IClass c = pc.GetClass(name, typeParameterCount); if (c != null) { return(c.DefaultReturnType); } // example where name is not found: pointers like System.Char* // or when the class is in a assembly that is not referenced } return(new GetClassReturnType(pc, name, typeParameterCount)); } }
public ReflectionMethod(MethodBase methodBase, ReflectionClass declaringType) : base(declaringType, methodBase is ConstructorInfo ? "#ctor" : methodBase.Name) { if (methodBase is MethodInfo) { this.ReturnType = ReflectionReturnType.Create(this, ((MethodInfo)methodBase).ReturnType, false); } else if (methodBase is ConstructorInfo) { this.ReturnType = DeclaringType.DefaultReturnType; } foreach (ParameterInfo paramInfo in methodBase.GetParameters()) { this.Parameters.Add(new ReflectionParameter(paramInfo, this)); } if (methodBase.IsGenericMethodDefinition) { foreach (Type g in methodBase.GetGenericArguments()) { this.TypeParameters.Add(new DefaultTypeParameter(this, g)); } int i = 0; foreach (Type g in methodBase.GetGenericArguments()) { declaringType.AddConstraintsFromType(this.TypeParameters[i++], g); } } if (methodBase.IsStatic) { foreach (CustomAttributeData data in CustomAttributeData.GetCustomAttributes(methodBase)) { string attributeName = data.Constructor.DeclaringType.FullName; if (attributeName == "System.Runtime.CompilerServices.ExtensionAttribute" || attributeName == "Boo.Lang.ExtensionAttribute") { this.IsExtensionMethod = true; } } } ModifierEnum modifiers = ModifierEnum.None; if (methodBase.IsStatic) { modifiers |= ModifierEnum.Static; } if (methodBase.IsPrivate) // I assume that private is used most and public last (at least should be) { modifiers |= ModifierEnum.Private; } else if (methodBase.IsFamily || methodBase.IsFamilyOrAssembly) { modifiers |= ModifierEnum.Protected; } else if (methodBase.IsPublic) { modifiers |= ModifierEnum.Public; } else { modifiers |= ModifierEnum.Internal; } if (methodBase.IsVirtual) { modifiers |= ModifierEnum.Virtual; } if (methodBase.IsAbstract) { modifiers |= ModifierEnum.Abstract; } this.Modifiers = modifiers; }