Ejemplo n.º 1
0
 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));
 }
Ejemplo n.º 2
0
 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();
 }
Ejemplo n.º 3
0
 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;
 }
Ejemplo n.º 4
0
 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;
 }
Ejemplo n.º 5
0
 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);
 }
Ejemplo n.º 6
0
 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;
 }
Ejemplo n.º 7
0
 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)));
 }
Ejemplo n.º 8
0
 private static bool IsGeneric(TypeName toFind)
 {
     return toFind.GenericArguments.Any();
 }
Ejemplo n.º 9
0
 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;
 }
Ejemplo n.º 10
0
 private Maybe<Type> LoadTypeFromAssembly(Assembly assembly, TypeName toFind)
 {
     var name = Generify(toFind);
     return assembly.GetType(name).AsMaybe().Combine(t=>InstantiateGeneric(t,toFind));
 }
Ejemplo n.º 11
0
 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);
 }
Ejemplo n.º 12
0
 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(']');
     }
 }
Ejemplo n.º 13
0
 public static TypeName CreateArrayOf(TypeName type, int array)
 {
     return new TypeName(type.rawName, type.AssemblyName, array, type.GenericArguments);
 }
Ejemplo n.º 14
0
 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));
 }
Ejemplo n.º 15
0
 /// <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;
 }
Ejemplo n.º 16
0
 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);
 }
Ejemplo n.º 17
0
 private SimpleClassDescription(TypeName fullName, Maybe<ITypeDescription> baseClass)
     : base(fullName,baseClass)
 {
 }
Ejemplo n.º 18
0
 private static TypeBuilder CreateType(ModuleBuilder modBuilder,
     TypeName className, Maybe<Type> baseType)
 {
     return modBuilder.DefineType(BuildName(className),
                                  TypeAttributes.Class | TypeAttributes.Public, baseType.GetValue(typeof(object)));
 }
Ejemplo n.º 19
0
 public static SimpleClassDescription Create(TypeName fullName,
     Func<ITypeDescription, IEnumerable<SimpleFieldDescription>> fieldGenerator)
 {
     return Create(fullName,Maybe<ITypeDescription>.Empty,fieldGenerator);
 }
Ejemplo n.º 20
0
 private static string BuildName(TypeName className)
 {
     return NameSpace + "." + CodeGenerationUtils.ClassName(className);
 }
Ejemplo n.º 21
0
 private Maybe<Type> InstantiateGeneric(Type type, TypeName toFind)
 {
     return IsGeneric(toFind) ? TryCreateGenericInstance(toFind, type) : type;
 }
Ejemplo n.º 22
0
 internal static string ClassName(TypeName className)
 {
     return className.NameWithGenerics + "_"
            + NormalizedAssemblyName(className);
 }
Ejemplo n.º 23
0
 private Maybe<Type> Resolve(TypeName toFind)
 {
     return FindInCurrentAppDomain(toFind)
         .Otherwise(()=>FindInGivenAssemblyPaths(toFind));
 }
Ejemplo n.º 24
0
 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;
 }
Ejemplo n.º 25
0
 private static string Generify(TypeName toFind)
 {
     return toFind.NameAndNamespace;
 }
Ejemplo n.º 26
0
 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));
 }
Ejemplo n.º 27
0
 internal static string NormalizedAssemblyName(TypeName className)
 {
     return className.AssemblyName.Replace('.', '_').Replace('-', '_');
 }
Ejemplo n.º 28
0
 private Maybe<IReflectClass> FindReflectClass(TypeName typeName,
     IReflectClass[] allKnownClasses)
 {
     return (from c in allKnownClasses
             where c.GetName() == typeName.FullName
             select c).FirstMaybe();
 }
Ejemplo n.º 29
0
 protected TypeDescriptionBase(TypeName typeName, Maybe<ITypeDescription> baseClass)
 {
     Name = typeName.NameWithGenerics.Split('.').Last();
     TypeName = typeName;
     BaseClass = baseClass;
 }
Ejemplo n.º 30
0
 private ArrayDescription(TypeName typeName, ITypeDescription innerType)
     : base(typeName, Maybe.From(KnownType.Array))
 {
     this.innerType = innerType;
 }