private Maybe<Type> FindInCurrentAppDomain(TypeName toFind) { var assembly = from a in AppDomain.CurrentDomain.GetAssemblies() where a.GetName().Name == toFind.AssemblyName select a; return assembly.FirstMaybe() .Combine(a => LoadTypeFromAssembly(a,toFind)); }
protected KnownType(Type typeInfo, TypeName theName, Func<ITypeDescription,IEnumerable<SimpleFieldDescription>> fieldsInitializer) { new { typeInfo }.CheckNotNull(); this.Name = theName.NameWithGenerics.Split('.').Last(); this.typeInfo = typeInfo; this.TypeName = theName; this.Fields = fieldsInitializer(this).ToList(); }
private ITypeDescription CreateArrayType(TypeName fullName, IReflectClass classInfo, Func<TypeName, Maybe<IReflectClass>> classLookup, IDictionary<string, ITypeDescription> knownTypes) { var innerType = GetOrCreateType(classInfo.GetComponentType(), classLookup, knownTypes); var type = ArrayDescription.Create(innerType, fullName.OrderOfArray); knownTypes[fullName.FullName] = type; return type; }
public static SimpleClassDescription Create(TypeName fullName, Maybe<ITypeDescription> baseClass, Func<ITypeDescription, IEnumerable<SimpleFieldDescription>> fieldGenerator) { if (fullName.OrderOfArray != 0) { throw new ArgumentException("Cannot be an array-type " + fullName.FullName); } var toConstruct = new SimpleClassDescription(fullName, baseClass); toConstruct.fields = fieldGenerator(toConstruct).ToArray(); return toConstruct; }
private static TypeName CreateType(string typeName, string assemblyName, int array, IEnumerable<TypeName> genericArgs = null) { if (null == genericArgs) { genericArgs = new TypeName[0]; } return TypeName.Create(typeName, assemblyName, genericArgs,array); }
private static IEnumerable<TypeName> CheckAndCreateGenericList(IEnumerable<TypeName> argList, TypeName lastArg, int expectedLength) { var result = argList.Concat(new[] {lastArg}).ToArray(); if (expectedLength != result.Length) { throw new ArgumentException( string.Format("argument-count hasn't the expected count. List-count {0}, expected {1}", result.Length, expectedLength)); } return result; }
private ITypeDescription GetOrCreateTypeByName(TypeName name, Func<TypeName, Maybe<IReflectClass>> classLookup, IDictionary<string, ITypeDescription> knownTypes) { var type = knownTypes.TryGet(name.FullName); if(type.HasValue) { return type.Value; } var systemType = typeResolver(name); return systemType.Convert(KnownType.Create) .GetValue(()=>classLookup(name) .Convert(n=>CreateType(name, n, classLookup, knownTypes)) .GetValue(()=>MockTypeFor(name,knownTypes))); }
private static bool IsGeneric(TypeName toFind) { return toFind.GenericArguments.Any(); }
private Maybe<Type> TryCreateGenericInstance(TypeName toFind, Type type) { if(toFind.GenericArguments.Any(t=>!t.HasValue)) { return type; } var types = toFind.GenericArguments.Select(t=>Resolve(t.Value)); if(types.All(t=>t.HasValue)) { return type.MakeGenericType(types.Select(t=>t.Value).ToArray()); } return Maybe<Type>.Empty; }
private Maybe<Type> LoadTypeFromAssembly(Assembly assembly, TypeName toFind) { var name = Generify(toFind); return assembly.GetType(name).AsMaybe().Combine(t=>InstantiateGeneric(t,toFind)); }
private bool Equals(TypeName other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return Equals(other.NameAndNamespace, NameAndNamespace) && Equals(other.AssemblyName, AssemblyName) && Equals(other.OrderOfArray, OrderOfArray) && other.GenericArguments.SequenceEqual(GenericArguments); }
private static void BuildGenericArguments(StringBuilder toBuild, TypeName typeName, Action<StringBuilder, Maybe<TypeName>> typeNameBuilder) { var arguments = typeName.GenericArguments.ToArray(); if (arguments.Any()) { toBuild.Append('['); for (var i = 0; i < arguments.Length; i++) { AddArg(toBuild, arguments[i], typeNameBuilder); if (IsNotLastArgument(i, arguments)) { toBuild.Append(", "); } } toBuild.Append(']'); } }
public static TypeName CreateArrayOf(TypeName type, int array) { return new TypeName(type.rawName, type.AssemblyName, array, type.GenericArguments); }
private SimpleFieldDescription CreateField(TypeName declaredOn, IReflectField field, Func<IReflectClass, ITypeDescription> typeLookUp) { var fieldType = typeLookUp(field.GetFieldType()); return SimpleFieldDescription.Create(field.GetName(), fieldType, IndexLookUp(declaredOn, field.GetName(), fieldType.TypeName)); }
/// <summary> /// For some types there is no type info available from db4o. Therefore we just create an empty class. /// </summary> private ITypeDescription MockTypeFor(TypeName typeName, IDictionary<string, ITypeDescription> knownTypes) { var type= SimpleClassDescription.Create(typeName, t => Enumerable.Empty<SimpleFieldDescription>()); knownTypes[typeName.FullName] = type; return type; }
private IndexingState IndexLookUp(TypeName declaringtype, string fieldName, TypeName fieldtype) { var storedInfo = cachedStoredClasses.TryGet(declaringtype.FullName); return storedInfo.Combine(sc => (from f in sc.Fields where f.FieldName == fieldName && f.TypeName == fieldtype.FullName select f).FirstMaybe()) .Convert(f => f.IndexState) .GetValue(IndexingState.Unknown); }
private SimpleClassDescription(TypeName fullName, Maybe<ITypeDescription> baseClass) : base(fullName,baseClass) { }
private static TypeBuilder CreateType(ModuleBuilder modBuilder, TypeName className, Maybe<Type> baseType) { return modBuilder.DefineType(BuildName(className), TypeAttributes.Class | TypeAttributes.Public, baseType.GetValue(typeof(object))); }
public static SimpleClassDescription Create(TypeName fullName, Func<ITypeDescription, IEnumerable<SimpleFieldDescription>> fieldGenerator) { return Create(fullName,Maybe<ITypeDescription>.Empty,fieldGenerator); }
private static string BuildName(TypeName className) { return NameSpace + "." + CodeGenerationUtils.ClassName(className); }
private Maybe<Type> InstantiateGeneric(Type type, TypeName toFind) { return IsGeneric(toFind) ? TryCreateGenericInstance(toFind, type) : type; }
internal static string ClassName(TypeName className) { return className.NameWithGenerics + "_" + NormalizedAssemblyName(className); }
private Maybe<Type> Resolve(TypeName toFind) { return FindInCurrentAppDomain(toFind) .Otherwise(()=>FindInGivenAssemblyPaths(toFind)); }
private Maybe<Type> FindInGivenAssemblyPaths(TypeName toFind) { foreach (var assemblyPath in filePaths) { var type = TryLoad(assemblyPath).Combine(a => LoadTypeFromAssembly(a,toFind)); if (type.HasValue) { return type; } } return Maybe<Type>.Empty; }
private static string Generify(TypeName toFind) { return toFind.NameAndNamespace; }
private IEnumerable<SimpleFieldDescription> ExtractFields(TypeName declaringTypeName, IReflectClass classInfo, Func<IReflectClass, ITypeDescription> typeLookUp) { return classInfo.GetDeclaredFields() .Where(f=>!(f is GenericVirtualField)) .Select(f => CreateField(declaringTypeName, f, typeLookUp)); }
internal static string NormalizedAssemblyName(TypeName className) { return className.AssemblyName.Replace('.', '_').Replace('-', '_'); }
private Maybe<IReflectClass> FindReflectClass(TypeName typeName, IReflectClass[] allKnownClasses) { return (from c in allKnownClasses where c.GetName() == typeName.FullName select c).FirstMaybe(); }
protected TypeDescriptionBase(TypeName typeName, Maybe<ITypeDescription> baseClass) { Name = typeName.NameWithGenerics.Split('.').Last(); TypeName = typeName; BaseClass = baseClass; }
private ArrayDescription(TypeName typeName, ITypeDescription innerType) : base(typeName, Maybe.From(KnownType.Array)) { this.innerType = innerType; }