//----------------------------------------------------------------------------------------------------------------------------------------------------- public static bool IsOfSignature(this MethodInfo method, Type returnType, params Type[] parameterTypes) { if (!method.ReturnType.IsGenericParameter && method.ReturnType != TypeTemplate.Resolve(returnType)) { return(false); } var actualParameterTypes = method.GetParameters().Select(p => p.ParameterType).ToArray(); if (actualParameterTypes.Length != parameterTypes.Length) { return(false); } for (int i = 0; i < parameterTypes.Length; i++) { if (actualParameterTypes[i].IsGenericParameter) { continue; } if (TypeTemplate.Resolve(parameterTypes[i]) != actualParameterTypes[i]) { return(false); } } return(true); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- public static MethodInfo ResolveMethodFromLambda(LambdaExpression lambda) { var originalDeclaration = ExtractMethodInfoFromLambda(lambda); var shouldResolveDeclaringType = TypeTemplate.IsTemplateType(originalDeclaration.DeclaringType); if (!originalDeclaration.IsGenericMethod && !shouldResolveDeclaringType) { return(originalDeclaration); } var resolvedDeclaration = originalDeclaration; if (shouldResolveDeclaringType) { var resolvedDeclaringType = TypeTemplate.Resolve(originalDeclaration.DeclaringType); var resolvedReturnType = TypeTemplate.Resolve(originalDeclaration.ReturnType); var resolvedParameterTypes = originalDeclaration.GetParameters().Select(p => TypeTemplate.Resolve(p.ParameterType)).ToArray(); resolvedDeclaration = TypeMemberCache.Of(resolvedDeclaringType) .Methods.Where(m => m.Name == originalDeclaration.Name) .OfSignature(resolvedReturnType, resolvedParameterTypes) .Single(); } if (resolvedDeclaration.IsGenericMethod) { var resolvedGenericArguments = originalDeclaration.GetGenericArguments().Select(TypeTemplate.Resolve).ToArray(); resolvedDeclaration = resolvedDeclaration.GetGenericMethodDefinition().MakeGenericMethod(resolvedGenericArguments); } return(resolvedDeclaration); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- public static FieldInfo ResolveFieldFromLambda(LambdaExpression lambda) { var originalFieldInfo = (FieldInfo)((MemberExpression)lambda.Body).Member; if (TypeTemplate.IsTemplateType(originalFieldInfo.DeclaringType)) { var resolvedDeclaringType = TypeTemplate.Resolve(originalFieldInfo.DeclaringType); return(TypeMemberCache.Of(resolvedDeclaringType).Fields.Single(m => m.Name == originalFieldInfo.Name)); } else { return(originalFieldInfo); } }
//----------------------------------------------------------------------------------------------------------------------------------------------------- public static PropertyInfo ResolvePropertyFromLambda(LambdaExpression lambda) { var propertyInfo = (PropertyInfo)((MemberExpression)lambda.Body).Member; if (TypeTemplate.IsTemplateType(propertyInfo.DeclaringType)) { var resolvedDeclaringType = TypeTemplate.Resolve(propertyInfo.DeclaringType); var resolvedPropertyType = TypeTemplate.Resolve(propertyInfo.PropertyType); var resolvedParameterTypes = propertyInfo.GetIndexParameters().Select(p => TypeTemplate.Resolve(p.ParameterType)).ToArray(); var resolvedPropertyInfo = TypeMemberCache.Of(resolvedDeclaringType) .Properties.Where(p => p.Name == propertyInfo.Name && p.DeclaringType == resolvedDeclaringType) .OfSignature(resolvedPropertyType, resolvedParameterTypes) .Single(); return(resolvedPropertyInfo); } else { return(propertyInfo); } }
//----------------------------------------------------------------------------------------------------------------------------------------------------- public static bool IsOfSignature(this PropertyInfo property, Type propertyType, params Type[] indexParameterTypes) { if (property.PropertyType != TypeTemplate.Resolve(propertyType)) { return(false); } var actualParameterTypes = property.GetIndexParameters().Select(p => p.ParameterType).ToArray(); if (actualParameterTypes.Length != indexParameterTypes.Length) { return(false); } for (int i = 0; i < indexParameterTypes.Length; i++) { if (TypeTemplate.Resolve(indexParameterTypes[i]) != actualParameterTypes[i]) { return(false); } } return(true); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- public IHappilClassBody <TBase> DeriveClassFrom <TBase>(string classFullName) { var typeAtributes = TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed | //TypeAttributes.BeforeFieldInit | TypeAttributes.AutoClass | TypeAttributes.AnsiClass; TypeBuilder typeBuilder = m_ModuleBuilder.DefineType(classFullName, typeAtributes, parent: TypeTemplate.Resolve <TBase>()); return(new HappilClass(this, typeBuilder).GetBody <TBase>()); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- public IHappilClassBody <object> DefineClass(string classFullName, Type baseType) { var typeAtributes = TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed | //TypeAttributes.BeforeFieldInit | TypeAttributes.AutoClass | TypeAttributes.AnsiClass; TypeBuilder typeBuilder = m_ModuleBuilder.DefineType(classFullName, typeAtributes, TypeTemplate.Resolve(baseType)); return(new HappilClass(this, typeBuilder).GetBody <object>()); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- public FieldSelector SelectFields <TField>(Func <FieldInfo, bool> where = null) { return(new FieldSelector(m_Fields.Where(f => f.FieldType == TypeTemplate.Resolve <TField>()), where)); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- public EventSelector SelectEvents <TEventHandler>(Func <EventInfo, bool> where = null) { return(new EventSelector(m_Events.Where(ev => ev.EventHandlerType == TypeTemplate.Resolve <TEventHandler>()), where)); }