void throwNotResolvable(object key, Type type) { if (key == null) { throw new ArgumentException($"Type {type.ToSimpleName()} cannot be resolved"); } throw new ArgumentException($"Type {type.ToSimpleName()} with key {key} cannot be resolved"); }
public static void GenerateLoadEx(this ITypeDescriptor descriptor, IILGen ilGenerator, Action<IILGen> pushReader, Action<IILGen> pushCtx, Action<IILGen> pushDescriptor, Type asType, ITypeConvertorGenerator convertorGenerator) { if (descriptor.StoredInline) { if (descriptor.LoadNeedsHelpWithConversion && asType!=typeof(object)) { var origType = descriptor.GetPreferedType(); descriptor.GenerateLoad(ilGenerator, pushReader, pushCtx, pushDescriptor, origType); if (origType != asType) { var conv = convertorGenerator.GenerateConversion(origType, asType); if (conv == null) throw new BTDBException("Don't know how to convert " + descriptor.Name + " from " + origType.ToSimpleName() + " to " + asType.ToSimpleName()); conv(ilGenerator); } } else { descriptor.GenerateLoad(ilGenerator, pushReader, pushCtx, pushDescriptor, asType); } } else { ilGenerator .Do(pushCtx) .Callvirt(() => default(ITypeBinaryDeserializerContext).LoadObject()); if (asType != typeof(object)) ilGenerator.Castclass(asType); } }
public IILLocal DeclareLocal(Type type, string name, bool pinned = false) { var localBuilder = _ilGenerator.DeclareLocal(type, pinned); if (name == null) { name = "local" + localBuilder.LocalIndex; } localBuilder.SetLocalSymInfo(name); _sourceCodeWriter.WriteLine(string.Format("{3}{0} {1} // index {2}", type.ToSimpleName(), name, localBuilder.LocalIndex, pinned ? "pinned " : "")); return new ILLocalImpl(localBuilder, name); }
public ILDynamicTypeDebugImpl(string name, Type baseType, Type[] interfaces) { _name = name; var uniqueName = UniqueName(name); _assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(uniqueName), AssemblyBuilderAccess.RunAndSave, "dynamicIL"); _moduleBuilder = _assemblyBuilder.DefineDynamicModule(uniqueName + ".dll", true); var sourceCodeFileName = Path.GetFullPath("dynamicIL/" + uniqueName + ".il"); _symbolDocumentWriter = _moduleBuilder.DefineDocument(sourceCodeFileName, SymDocumentType.Text, SymLanguageType.ILAssembly, SymLanguageVendor.Microsoft); _sourceCodeWriter = new SourceCodeWriter(sourceCodeFileName, _symbolDocumentWriter); _sourceCodeWriter.WriteLine(string.Format("class {0} : {1}{2}", name, baseType.ToSimpleName(), string.Concat(interfaces.Select(i => ", " + i.ToSimpleName())))); _sourceCodeWriter.OpenScope(); _typeBuilder = _moduleBuilder.DefineType(name, TypeAttributes.Public, baseType, interfaces); _forbidenInstructions = new ILGenForbidenInstructionsCheating(_typeBuilder); }
public ILDynamicTypeDebugImpl(string name, Type baseType, Type[] interfaces) { _name = name; var uniqueName = UniqueName(name, 259 - (DynamicILDirectoryPath.DynamicIL.Length + 1 + 4)); _assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(uniqueName), AssemblyBuilderAccess.RunAndSave, DynamicILDirectoryPath.DynamicIL); _moduleBuilder = _assemblyBuilder.DefineDynamicModule(uniqueName + ".dll", true); var sourceCodeFileName = Path.Combine(DynamicILDirectoryPath.DynamicIL, uniqueName + ".il"); _symbolDocumentWriter = _moduleBuilder.DefineDocument(sourceCodeFileName, SymDocumentType.Text, SymLanguageType.ILAssembly, SymLanguageVendor.Microsoft); _sourceCodeWriter = new SourceCodeWriter(sourceCodeFileName, _symbolDocumentWriter); _sourceCodeWriter.WriteLine( $"class {name} : {baseType.ToSimpleName()}{string.Concat(interfaces.Select(i => ", " + i.ToSimpleName()))}"); _sourceCodeWriter.OpenScope(); _typeBuilder = _moduleBuilder.DefineType(name, TypeAttributes.Public, baseType, interfaces); _forbidenInstructions = new ILGenForbidenInstructionsCheating(_typeBuilder); }
public ITypeDescriptor Create(Type type) { SerializerTypeInfo result; if (_typeOrDescriptor2Info.TryGetValue(type, out result)) return result.Descriptor; if (_typeOrDescriptor2InfoNew.TryGetValue(type, out result)) return result.Descriptor; ITypeDescriptor desc = null; if (!type.IsSubclassOf(typeof(Delegate))) { if (type.IsGenericType) { if (type.GetGenericTypeDefinition().InheritsOrImplements(typeof(IList<>))) { desc = new ListTypeDescriptor(this, type); } else if (type.GetGenericTypeDefinition().InheritsOrImplements(typeof(IDictionary<,>))) { desc = new DictionaryTypeDescriptor(this, type); } } else if (type.IsArray) { desc = new ListTypeDescriptor(this, type); } else if (type.IsEnum) { desc = new EnumTypeDescriptor(this, type); } else { desc = new ObjectTypeDescriptor(this, type); } } if (desc == null) throw new BTDBException("Don't know how to serialize type " + type.ToSimpleName()); result = new SerializerTypeInfo { Id = 0, Descriptor = desc }; _typeOrDescriptor2InfoNew[desc] = result; _typeOrDescriptor2InfoNew[type] = result; if (!desc.FinishBuildFromType(this)) { throw new BTDBException("Don't know how to serialize type " + type.ToSimpleName()); } return desc; }
public void Emit(OpCode opCode, Type type) { _sourceCodeWriter.MarkAndWriteLine(_ilGenerator, string.Format("{0} {1}", opCode, type.ToSimpleName())); _ilGenerator.Emit(opCode, type); }
internal void StartMethod(string name, Type returns, Type[] parameters, MethodAttributes methodAttributes) { var before = ""; var firstArgIdx = 1; if (methodAttributes.HasFlag(MethodAttributes.Public)) before += "public "; if (methodAttributes.HasFlag(MethodAttributes.Static)) { firstArgIdx = 0; before += "static "; } if (methodAttributes.HasFlag(MethodAttributes.Virtual)) before += "virtual "; switch (parameters.Length) { case 0: WriteLine(String.Format("{2}{0} {1}()", returns.ToSimpleName(), name, before)); break; case 1: WriteLine(String.Format("{2}{0} {1}({3} arg{4})", returns.ToSimpleName(), name, before, parameters[0].ToSimpleName(), firstArgIdx)); break; default: WriteLine(String.Format("{2}{0} {1}(", returns.ToSimpleName(), name, before)); Indent++; int idx = 0; foreach (var par in parameters) { WriteLine(String.Format("{0} arg{1}{2}", par.ToSimpleName(), idx + firstArgIdx, idx + 1 == parameters.Length ? ')' : ',')); idx++; } Indent--; break; } OpenScope(); }
public ICRegILGen ResolveNeedBy(Type type, object key) { ICReg registration; if (_container.Registrations.TryGetValue(new KeyAndType(key, type), out registration)) { var multi = registration as ICRegMulti; if (multi != null) { registration = ChooseFromMulti(multi, false) ?? multi.ChosenOne; } } if (registration == null) { if (type.IsDelegate()) { var resultType = type.GetMethod("Invoke").ReturnType; var nestedRegistration = ResolveNeedBy(resultType, key); if (nestedRegistration == null) return null; registration = new DelegateImpl(key, type, nestedRegistration); } else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Lazy<>)) { var resultType = type.GetGenericArguments()[0]; var nestedRegistration = ResolveNeedBy(resultType, key); if (nestedRegistration == null) return null; registration = new LazyImpl(type, nestedRegistration); } else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>)) { var resultType = type.GetGenericArguments()[0]; var child = MakeEnumEnabledChild(); var nestedRegistration = child.ResolveNeedBy(resultType, key); if (nestedRegistration == null) { if (key != null) return null; registration = new EmptyEnumerableImpl(type, resultType); } else { registration = new EnumerableImpl(key, type, resultType, child, nestedRegistration); } } else if (type.IsArray && type.GetArrayRank() == 1) { var resultType = type.GetElementType(); var child = MakeEnumEnabledChild(); var nestedRegistration = child.ResolveNeedBy(resultType, key); if (nestedRegistration == null) return null; registration = new EnumerableImpl(key, type, resultType, child, nestedRegistration); } else if (type.IsGenericType && TupleTypes.Contains(type.GetGenericTypeDefinition())) { registration = new AlwaysNewImpl(type, type.GetConstructors()[0]); } } if (registration != null) { var result = registration as ICRegILGen; if (result != null) return result; throw new ArgumentException("Builder for " + type.ToSimpleName() + " is not ILGen capable"); } return null; }
public void Emit(OpCode opCode, Type type) { _sourceCodeWriter.MarkAndWriteLine(_ilGenerator, $"{opCode} {type.ToSimpleName()}"); _ilGenerator.Emit(opCode, type); }
public FieldBuilder DefineField(string name, Type type, FieldAttributes fieldAttributes) { CloseInScope(); _sourceCodeWriter.WriteLine($"{type.ToSimpleName()} {name};"); return _typeBuilder.DefineField(name, type, fieldAttributes); }
public IILEvent DefineEvent(string name, EventAttributes eventAttributes, Type type) { CloseInScope(); _sourceCodeWriter.WriteLine($"event {type.ToSimpleName()} {name};"); return new ILEventDebugImpl(_typeBuilder.DefineEvent(name, eventAttributes, type)); }
public FieldBuilder DefineField(string name, Type type, FieldAttributes fieldAttributes) { CloseInScope(); _sourceCodeWriter.WriteLine(string.Format("{0} {1};", type.ToSimpleName(), name)); return _typeBuilder.DefineField(name, type, fieldAttributes); }
void throwNotResolvable(object key, Type type) { if (key == null) { throw new ArgumentException(string.Format("Type {0} cannot be resolved", type.ToSimpleName())); } throw new ArgumentException(string.Format("Type {0} with key {1} cannot be resolved", type.ToSimpleName(), key)); }