Ejemplo n.º 1
0
        private T FindExactMatch <T>(AssemblyData g, IDictionary <Type, T> dictionary, Type type)
        {
            T result;

            if (type == null)
            {
                return(default(T));
            }

            if (dictionary.TryGetValue(type, out result))
            {
                return(result);
            }

            if (type.IsGenericType && dictionary.TryGetValue(type.GetGenericTypeDefinition(), out result))
            {
                GenericTypes = type.GetGenericArguments().Select(t => g.GetPropertyType(t)).ToArray();
                return(result);
            }

            if (type.IsArray && dictionary.TryGetValue(typeof(Array), out result))
            {
                GenericTypes = new [] { g.GetPropertyType(type.GetElementType()) };
                return(result);
            }

            return(default(T));
        }
Ejemplo n.º 2
0
        public TypeData(AssemblyData g, Type type)
        {
            this.Type               = type;
            this.Name               = type.Name;
            this.Namespace          = type.Namespace;
            this.SupportsInitialize = typeof(ISupportInitialize).IsAssignableFrom(type);
            this.Properties         = new ReadOnlyCollection <PropertyData>((
                                                                                from member in type.GetMembers(BindingFlags)
                                                                                select new PropertyData(g, member)).Where(x => x.IsSerializable).ToArray());

            var constructors = type.GetConstructors(BindingFlags);

            this.HasDefaultConstructor         = constructors.Any(x => x.GetParameters().Length == 0);
            this.HasServiceProviderConstructor = constructors.Any(x => x.GetParameters().Length == 1 && x.GetParameters()[0].ParameterType == typeof(IServiceProvider));

            if (!HasDefaultConstructor && !HasServiceProviderConstructor)
            {
                foreach (var constructor in constructors)
                {
                    var parameters = constructor.GetParameters();
                    if (parameters.Length < 1)
                    {
                        continue;
                    }
                    if (parameters.All(p => g.ConstructorMethods.ContainsKey(p.ParameterType)))
                    {
                        ConstructorParameters = parameters.Select(p => g.ConstructorMethods[p.ParameterType]).ToArray();
                        break;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public ReadMethodData(AssemblyData g, MethodInfo method, bool isExtensionMethod)
            : base(g, method, isExtensionMethod)
        {
            if (isExtensionMethod)
            {
                this.HasReturnValue = method.ReturnType != typeof(void);

                var parameters = method.GetParameters();
                if (!method.IsGenericMethodDefinition)
                {
                    this.IsValid =
                        (parameters.Length == 1 ||
                         (parameters.Length == 2 && parameters[1].ParameterType == typeof(IServiceProvider) && this.HasReturnValue) ||
                         (parameters.Length == 3 && parameters[2].ParameterType == typeof(IServiceProvider)));
                    this.HasExistingInstance = parameters.Length == 3;
                    this.HasServiceProvider  = IsValid && parameters.Length >= 2;
                    this.TargetType          = this.HasReturnValue ? method.ReturnType : parameters[1].ParameterType;
                }
                else
                {
                    var genericArguments = method.GetGenericArguments();
                    if (this.IsValid = (
                            parameters.Length == 2 + genericArguments.Length &&
                            parameters.Skip(2).All(p => p.ParameterType.IsGenericType &&
                                                   p.ParameterType.GetGenericTypeDefinition() == typeof(Func <,>))))
                    {
                        this.HasServiceProvider  = false;
                        this.IsGeneric           = true;
                        this.HasExistingInstance = true;
                        this.TargetType          = this.HasReturnValue ?
                                                   (method.ReturnType.IsArray ? typeof(Array) : method.ReturnType.GetGenericTypeDefinition())
                            : parameters[1].ParameterType.GetGenericTypeDefinition();
                    }
                    else if (this.IsValid = (
                                 parameters.Length == 1 + genericArguments.Length &&
                                 parameters.Skip(1).All(p => p.ParameterType.IsGenericType &&
                                                        p.ParameterType.GetGenericTypeDefinition() == typeof(Func <,>))))
                    {
                        this.HasServiceProvider = false;
                        this.IsGeneric          = true;
                        this.TargetType         = method.ReturnType.IsArray ? typeof(Array) : method.ReturnType.GetGenericTypeDefinition();
                    }
                }
            }
            else
            {
                this.HasReturnValue = true;
                this.TargetType     = method.ReturnType;
            }

            if (!this.HasReturnValue && !this.HasExistingInstance)
            {
                this.IsValid = false;
            }

            this.ReadIntoExistingInstance = !this.HasReturnValue && this.HasExistingInstance;
            this.MethodName = isExtensionMethod ? method.DeclaringType.FullName + "." + method.Name : method.Name;
        }
Ejemplo n.º 4
0
        public PropertyData(AssemblyData g, MemberInfo member)
        {
            this.Member = member;
            this.Name   = member.Name;

            if (HasNotBinarySerializableAttribute(member))
            {
                return;
            }

            var forceSerialize = HasBinarySerializableAttribute(member);

            var field = member as FieldInfo;

            if (field != null)
            {
                if (forceSerialize || (field.IsPublic && CanSerialize(field.FieldType)))
                {
                    this.Type           = g.GetPropertyType(field.FieldType);
                    this.IsSerializable = this.Type.Read != null && this.Type.Write != null;
                }
            }

            var property = member as PropertyInfo;

            if (property != null)
            {
                if (property.GetIndexParameters().Length > 0 || !CanSerialize(property.PropertyType))
                {
                    return;
                }

                var get = property.GetGetMethod(true);
                if (get == null)
                {
                    return;
                }

                if (forceSerialize || get.IsPublic)
                {
                    this.Type           = g.GetPropertyType(property.PropertyType);
                    this.IsSerializable = this.Type.Read != null && this.Type.Write != null;
                    if (this.IsSerializable)
                    {
                        var set = property.GetSetMethod(true);
                        if (set == null)
                        {
                            this.IsSerializable = this.Type.Read.ReadIntoExistingInstance;
                        }
                        else if (!set.IsPublic)
                        {
                            this.IsSerializable = this.Type.Read.ReadIntoExistingInstance || forceSerialize;
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public MethodData(AssemblyData g, MethodInfo method, bool isExtensionMethod)
        {
            this.MethodInfo        = method;
            this.IsExtensionMethod = isExtensionMethod;

            if (!g.AssemblyOrders.TryGetValue(method.DeclaringType.Assembly, out AssemblyOrder))
            {
                AssemblyOrder = int.MinValue;
            }
        }
Ejemplo n.º 6
0
        private T FindBestMatch <T>(AssemblyData g, IDictionary <Type, T> dictionary, Type type)
        {
            if (type == null)
            {
                return(default(T));
            }

            if (type == typeof(object))
            {
                return(FindExactMatch(g, dictionary, typeof(object)));
            }

            // BFS Interface Traversal
            Queue <Type> typeQueue = new Queue <Type>();

            foreach (var topLevelInterface in GetTopLevelInterfaces(type))
            {
                typeQueue.Enqueue(topLevelInterface);
            }

            var baseType = type.BaseType;

            while (baseType != null && baseType != typeof(object))
            {
                var result = FindExactMatch(g, dictionary, baseType);
                if (result != null)
                {
                    return(result);
                }
                foreach (var topLevelInterface in GetTopLevelInterfaces(baseType))
                {
                    typeQueue.Enqueue(topLevelInterface);
                }
                baseType = baseType.BaseType;
            }

            while (typeQueue.Count > 0)
            {
                var currentType = typeQueue.Dequeue();
                var result      = FindExactMatch(g, dictionary, currentType);
                if (result != null)
                {
                    return(result);
                }
                foreach (var topLevelInterface in GetTopLevelInterfaces(currentType))
                {
                    typeQueue.Enqueue(topLevelInterface);
                }
            }

            return(FindExactMatch(g, dictionary, typeof(object)));
        }
Ejemplo n.º 7
0
 public PropertyTypeData(AssemblyData g, Type type)
 {
     this.Type = type;
     if (type.IsEnum)
     {
         g.GetEnumTypeAndOffset(type, out this.EnumType, out this.EnumOffset);
     }
     if ((this.Read = FindExactMatch(g, g.BinaryReaderMethods, type)) == null)
     {
         this.Read = FindBestMatch(g, g.BinaryReaderMethods, type);
     }
     if ((this.Write = FindExactMatch(g, g.BinaryWriterMethods, type)) == null)
     {
         this.Write = FindBestMatch(g, g.BinaryWriterMethods, type);
     }
 }
Ejemplo n.º 8
0
        public static string Generate(string target, string workingDirectory, bool delaySign, string keyFile, string[] references, Action <CompilerError> onError)
        {
            foreach (var reference in references)
            {
                Assembly.LoadFrom(reference);
            }

            //System.Diagnostics.Debugger.Launch();

            var g = new AssemblyData(Assembly.LoadFrom(target));

            if (g.Types.Count <= 0)
            {
                return(null);
            }

            var reader = new BinaryObjectReader(g);
            var writer = new BinaryObjectWriter(g);

            var readerCode = reader.TransformText();
            var writerCode = writer.TransformText();

            return(Compile(g.Assembly, workingDirectory, delaySign, keyFile, references, onError, BuildAssemblyInfo(g.Assembly), readerCode, writerCode));
        }
Ejemplo n.º 9
0
 public WriteMethodData(AssemblyData g, MethodInfo method, bool isExtensionMethod)
     : base(g, method, isExtensionMethod)
 {
     if (isExtensionMethod)
     {
         var parameters = method.GetParameters();
         if (!method.IsGenericMethodDefinition)
         {
             if (this.IsValid = method.ReturnType == typeof(void) &&
                                (parameters.Length == 2 ||
                                 (parameters.Length == 3 && parameters[2].ParameterType == typeof(IServiceProvider))))
             {
                 this.HasServiceProvider = parameters.Length == 3;
                 this.TargetType         = parameters[1].ParameterType;
             }
         }
         else
         {
             var genericArguments = method.GetGenericArguments();
             if (this.IsValid = (method.ReturnType == typeof(void) &&
                                 parameters.Length == 2 + genericArguments.Length &&
                                 parameters.Skip(2).All(p => p.ParameterType.IsGenericType &&
                                                        p.ParameterType.GetGenericTypeDefinition() == typeof(Action <>))))
             {
                 this.HasServiceProvider = false;
                 this.IsGeneric          = true;
                 this.TargetType         = parameters[1].ParameterType.IsArray ? typeof(Array) : parameters[1].ParameterType.GetGenericTypeDefinition();
             }
         }
     }
     else
     {
         this.TargetType = method.GetParameters()[0].ParameterType;
     }
     this.MethodName = isExtensionMethod ? method.DeclaringType.FullName + "." + method.Name : method.Name;
 }
Ejemplo n.º 10
0
 public BinaryObjectReader(AssemblyData g)
 {
     this.g = g;
 }
Ejemplo n.º 11
0
 public BinaryObjectWriter(AssemblyData g)
 {
     this.g = g;
 }