protected void RefreshResolveTypeInterceptors() { ResolveTypesInterceptors = new List <IResolveTypeInterceptor>(); DynamicAssemblies.ForEach(da => { ResolveTypesInterceptors.AddRange(da.GetTypes() .Where(t => t.IsClass && typeof(IResolveTypeInterceptor).IsAssignableFrom(t)) .Select(t => (IResolveTypeInterceptor)Activator.CreateInstance(t, (IGenerator)this)) ); }); }
protected virtual List <Assembly> LoadDynamicAssemblies() { var ret = new List <Assembly>(); Options.DynamicAssemblies?.ForEach(da => { var a = Assembly.LoadFile(da); DynamicAssemblies.Add(a); }); return(ret); }
private void ContextHook() { DynamicAssemblies.ForEach(a => { var contextServices = a.GetTypes() .Where(t => t.IsClass && typeof(IContextInterceptor).IsAssignableFrom(t)) .Select(t => (IContextInterceptor)Activator.CreateInstance(t, (IGenerator)this)) .ToList(); contextServices.ForEach(c => c.InterceptContext()); }); }
private void EachTableHooks() { DynamicAssemblies.ForEach(a => { var eachTableServices = a.GetTypes() .Where(t => t.IsClass && typeof(ITableInterceptor).IsAssignableFrom(t)) .Select(t => (ITableInterceptor)Activator.CreateInstance(t, (IGenerator)this)) .ToList(); TablesToGenerate.ForEach(table => { eachTableServices.ForEach(t => t.InterceptTable(table)); }); }); }
internal void GenerateSupportedTypes(Type[] types) { _writer.Write("public override "); _writer.Write(typeof(bool).FullName); _writer.Write(" CanSerialize("); _writer.Write(typeof(Type).FullName); _writer.WriteLine(" type) {"); _writer.Indent++; Hashtable uniqueTypes = new Hashtable(); for (int i = 0; i < types.Length; i++) { Type type = types[i]; if (type == null) { continue; } TypeInfo info = type.GetTypeInfo(); if (!info.IsPublic && !info.IsNestedPublic) { continue; } if (uniqueTypes[type] != null) { continue; } if (DynamicAssemblies.IsTypeDynamic(type)) { continue; } if (info.IsGenericType || info.ContainsGenericParameters && DynamicAssemblies.IsTypeDynamic(type.GetGenericArguments())) { continue; } uniqueTypes[type] = type; _writer.Write("if (type == typeof("); _writer.Write(CodeIdentifier.GetCSharpName(type)); _writer.WriteLine(")) return true;"); } _writer.WriteLine("return false;"); _writer.Indent--; _writer.WriteLine("}"); }
//GenerateGetSerializer(serializers, xmlMappings); private void GenerateGetSerializer(Hashtable serializers, XmlMapping[] xmlMappings) { _writer.Write("public override "); _writer.Write(typeof(XmlSerializer).FullName); _writer.Write(" GetSerializer("); _writer.Write(typeof(Type).FullName); _writer.WriteLine(" type) {"); _writer.Indent++; for (int i = 0; i < xmlMappings.Length; i++) { if (xmlMappings[i] is XmlTypeMapping) { Type type = xmlMappings[i].Accessor.Mapping.TypeDesc.Type; if (type == null) { continue; } if (!type.IsPublic && !type.IsNestedPublic) { continue; } if (DynamicAssemblies.IsTypeDynamic(type)) { continue; } if (type.IsGenericType || type.ContainsGenericParameters && DynamicAssemblies.IsTypeDynamic(type.GetGenericArguments())) { continue; } _writer.Write("if (type == typeof("); _writer.Write(CodeIdentifier.GetCSharpName(type)); _writer.Write(")) return new "); _writer.Write((string)serializers[xmlMappings[i].Key]); _writer.WriteLine("();"); } } _writer.WriteLine("return null;"); _writer.Indent--; _writer.WriteLine("}"); }
internal static bool GenerateSerializerToStream(XmlMapping[] xmlMappings, Type[] types, string defaultNamespace, Assembly assembly, Hashtable assemblies, Stream stream) { var compiler = new Compiler(); try { var scopeTable = new Hashtable(); foreach (XmlMapping mapping in xmlMappings) { scopeTable[mapping.Scope] = mapping; } var scopes = new TypeScope[scopeTable.Keys.Count]; scopeTable.Keys.CopyTo(scopes, 0); assemblies.Clear(); var importedTypes = new Hashtable(); foreach (TypeScope scope in scopes) { foreach (Type t in scope.Types) { compiler.AddImport(t, importedTypes); Assembly a = t.Assembly; string name = a.FullName; if (assemblies[name] != null) { continue; } if (!a.GlobalAssemblyCache) { assemblies[name] = a; } } } for (int i = 0; i < types.Length; i++) { compiler.AddImport(types[i], importedTypes); } compiler.AddImport(typeof(object).Assembly); compiler.AddImport(typeof(System.Xml.Serialization.XmlSerializer).Assembly); var writer = new IndentedWriter(compiler.Source, false); writer.WriteLine("[assembly:System.Security.AllowPartiallyTrustedCallers()]"); writer.WriteLine("[assembly:System.Security.SecurityTransparent()]"); writer.WriteLine("[assembly:System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)]"); if (assembly != null && types.Length > 0) { for (int i = 0; i < types.Length; i++) { Type type = types[i]; if (type == null) { continue; } if (DynamicAssemblies.IsTypeDynamic(type)) { throw new InvalidOperationException(SR.Format(SR.XmlPregenTypeDynamic, types[i].FullName)); } } writer.Write("[assembly:"); writer.Write(typeof(XmlSerializerVersionAttribute).FullName); writer.Write("("); writer.Write("ParentAssemblyId="); ReflectionAwareCodeGen.WriteQuotedCSharpString(writer, GenerateAssemblyId(types[0])); writer.Write(", Version="); ReflectionAwareCodeGen.WriteQuotedCSharpString(writer, ThisAssembly.Version); if (defaultNamespace != null) { writer.Write(", Namespace="); ReflectionAwareCodeGen.WriteQuotedCSharpString(writer, defaultNamespace); } writer.WriteLine(")]"); } var classes = new CodeIdentifiers(); classes.AddUnique("XmlSerializationWriter", "XmlSerializationWriter"); classes.AddUnique("XmlSerializationReader", "XmlSerializationReader"); string suffix = null; if (types != null && types.Length == 1 && types[0] != null) { suffix = CodeIdentifier.MakeValid(types[0].Name); if (types[0].IsArray) { suffix += "Array"; } } writer.WriteLine("namespace " + GeneratedAssemblyNamespace + " {"); writer.Indent++; writer.WriteLine(); string writerClass = "XmlSerializationWriter" + suffix; writerClass = classes.AddUnique(writerClass, writerClass); var writerCodeGen = new XmlSerializationWriterCodeGen(writer, scopes, "public", writerClass); writerCodeGen.GenerateBegin(); string[] writeMethodNames = new string[xmlMappings.Length]; for (int i = 0; i < xmlMappings.Length; i++) { writeMethodNames[i] = writerCodeGen.GenerateElement(xmlMappings[i]); } writerCodeGen.GenerateEnd(); writer.WriteLine(); string readerClass = "XmlSerializationReader" + suffix; readerClass = classes.AddUnique(readerClass, readerClass); var readerCodeGen = new XmlSerializationReaderCodeGen(writer, scopes, "public", readerClass); readerCodeGen.GenerateBegin(); string[] readMethodNames = new string[xmlMappings.Length]; for (int i = 0; i < xmlMappings.Length; i++) { readMethodNames[i] = readerCodeGen.GenerateElement(xmlMappings[i]); } readerCodeGen.GenerateEnd(readMethodNames, xmlMappings, types); string baseSerializer = readerCodeGen.GenerateBaseSerializer("XmlSerializer1", readerClass, writerClass, classes); var serializers = new Hashtable(); for (int i = 0; i < xmlMappings.Length; i++) { if (serializers[xmlMappings[i].Key] == null) { serializers[xmlMappings[i].Key] = readerCodeGen.GenerateTypedSerializer(readMethodNames[i], writeMethodNames[i], xmlMappings[i], classes, baseSerializer, readerClass, writerClass); } } readerCodeGen.GenerateSerializerContract("XmlSerializerContract", xmlMappings, types, readerClass, readMethodNames, writerClass, writeMethodNames, serializers); writer.Indent--; writer.WriteLine("}"); string codecontent = compiler.Source.ToString(); Byte[] info = new UTF8Encoding(true).GetBytes(codecontent); stream.Write(info, 0, info.Length); stream.Flush(); return(true); } finally { compiler.Close(); } }
// SxS: This method does not take any resource name and does not expose any resources to the caller. // It's OK to suppress the SxS warning. internal void AddImport(Type type, Hashtable types) { if (type == null) { return; } if (TypeScope.IsKnownType(type)) { return; } if (types[type] != null) { return; } types[type] = type; Type baseType = type.BaseType; if (baseType != null) { AddImport(baseType, types); } Type declaringType = type.DeclaringType; if (declaringType != null) { AddImport(declaringType, types); } foreach (Type intf in type.GetInterfaces()) { AddImport(intf, types); } ConstructorInfo[] ctors = type.GetConstructors(); for (int i = 0; i < ctors.Length; i++) { ParameterInfo[] parms = ctors[i].GetParameters(); for (int j = 0; j < parms.Length; j++) { AddImport(parms[j].ParameterType, types); } } if (type.IsGenericType) { Type[] arguments = type.GetGenericArguments(); for (int i = 0; i < arguments.Length; i++) { AddImport(arguments[i], types); } } Module module = type.Module; Assembly assembly = module.Assembly; if (DynamicAssemblies.IsTypeDynamic(type)) { DynamicAssemblies.Add(assembly); return; } object[] typeForwardedFromAttribute = type.GetCustomAttributes(typeof(TypeForwardedFromAttribute), false); if (typeForwardedFromAttribute.Length > 0) { TypeForwardedFromAttribute originalAssemblyInfo = typeForwardedFromAttribute[0] as TypeForwardedFromAttribute; Assembly originalAssembly = Assembly.Load(new AssemblyName(originalAssemblyInfo.AssemblyFullName)); //_imports[originalAssembly] = originalAssembly.Location; } //_imports[assembly] = assembly.Location; }