//----------------------------------------------------------------------------------------------------------------------------------------------------- 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 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); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- void IObjectFactoryConvention.Apply(ObjectFactoryContext context) { if (m_Will.HasFlag(Will.InspectDeclaration)) { OnInspectDeclaration(context); } if (m_Will.HasFlag(Will.ImplementBaseClass)) { OnImplementBaseClass(context.CreateImplementationWriter <TypeTemplate.TBase>()); } if (m_Will.HasFlag(Will.ImplementPrimaryInterface) && context.TypeKey.PrimaryInterface != null && ShouldImplementInterface(context, context.TypeKey.PrimaryInterface)) { using (TypeTemplate.CreateScope <TypeTemplate.TInterface>(context.TypeKey.PrimaryInterface)) { OnImplementPrimaryInterface(context.CreateImplementationWriter <TypeTemplate.TInterface>()); } } if (m_Will.HasFlag(Will.ImplementAnySecondaryInterface)) { foreach (var secondaryInterface in context.TypeKey.SecondaryInterfaces .Where(t => ShouldImplementInterface(context, t))) { using (TypeTemplate.CreateScope <TypeTemplate.TInterface>(secondaryInterface)) { OnImplementAnySecondaryInterface(context.CreateImplementationWriter <TypeTemplate.TInterface>()); } } } if (m_Will.HasFlag(Will.ImplementAnyInterface)) { foreach (var secondaryInterface in context.TypeKey.GetAllInterfaces() .Where(t => ShouldImplementInterface(context, t))) { using (TypeTemplate.CreateScope <TypeTemplate.TInterface>(secondaryInterface)) { OnImplementAnyInterface(context.CreateImplementationWriter <TypeTemplate.TInterface>()); } } } if (m_Will.HasFlag(Will.ImplementAnyBaseType)) { foreach (var secondaryInterface in context.TypeKey.GetAllAncestorTypes() .Where(t => !t.IsInterface || ShouldImplementInterface(context, t))) { using (TypeTemplate.CreateScope <TypeTemplate.TBase>(secondaryInterface)) { OnImplementAnyBaseType(context.CreateImplementationWriter <TypeTemplate.TBase>()); } } } }
//------------------------------------------------------------------------------------------------------------------------------------------------- protected override IDisposable CreateTemplateScope(MethodInfo method) { var parameterTypes = method.GetParameters().Select(p => p.ParameterType).ToArray(); var templateTypePairs = new Type[2 * (1 + parameterTypes.Length)]; templateTypePairs[0] = typeof(TypeTemplate.TReturn); templateTypePairs[1] = method.ReturnType; TypeTemplate.BuildArgumentsTypePairs(parameterTypes, templateTypePairs, arrayStartIndex: 2); return(TypeTemplate.CreateScope(templateTypePairs)); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- 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); } }
//------------------------------------------------------------------------------------------------------------------------------------------------- protected override IDisposable CreateTemplateScope(PropertyInfo property) { var parameterTypes = property.GetIndexParameters().Select(p => p.ParameterType).ToArray(); var templateTypePairs = new Type[2 * (1 + parameterTypes.Length)]; templateTypePairs[0] = typeof(TypeTemplate.TProperty); templateTypePairs[1] = property.PropertyType; if (parameterTypes.Length > 0) { TypeTemplate.BuildArgumentsTypePairs(parameterTypes, templateTypePairs, arrayStartIndex: 2); } return(TypeTemplate.CreateScope(templateTypePairs)); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- 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 IDisposable CreateTypeTemplateScope() { var typePairCount = 1 + // TBase (m_PrimaryInterface != null ? 1 : 0) + // TPrimary (m_SecondaryInterfacesArray != null ? Math.Min(m_SecondaryInterfacesArray.Length, 2) : 0); // TSecondary1, TSecondary2 var typePairs = new Type[typePairCount * 2]; var index = 0; typePairs[index++] = typeof(TypeTemplate.TBase); typePairs[index++] = m_BaseType; if (m_PrimaryInterface != null) { typePairs[index++] = typeof(TypeTemplate.TPrimary); typePairs[index++] = m_PrimaryInterface; } if (m_SecondaryInterfacesArray != null) { if (m_SecondaryInterfacesArray.Length >= 1) { typePairs[index++] = typeof(TypeTemplate.TSecondary1); typePairs[index++] = m_SecondaryInterfacesArray[0]; } if (m_SecondaryInterfacesArray.Length >= 2) { typePairs[index++] = typeof(TypeTemplate.TSecondary2); typePairs[index++] = m_SecondaryInterfacesArray[1]; } } return(TypeTemplate.CreateScope(typePairs)); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- 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>()); }
//------------------------------------------------------------------------------------------------------------------------------------------------- protected override IDisposable CreateTemplateScope(FieldInfo field) { return(TypeTemplate.CreateScope <TypeTemplate.TField>(field.FieldType)); }
//------------------------------------------------------------------------------------------------------------------------------------------------- protected override IDisposable CreateTemplateScope(EventInfo @event) { return(TypeTemplate.CreateScope <TypeTemplate.TEventHandler>(@event.EventHandlerType)); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- 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)); }