public DomainEntityFactory(Action <T> initializer = null) { var concreteType = ProxyTypeBuilder.BuildDomainEntityProxy <T>(); New = new TypeConstructor <T, TArg1, TArg2>(concreteType, x => { initializer?.Invoke(x); x.OnCreated(); }); Existing = new TypeConstructor <T, TArg1, TArg2>(concreteType, x => { initializer?.Invoke(x); x.OnLoaded(); }); }
public IType makeTypeI(Type boogieType, Context context) { Debug.Assert(boogieType != null); IType result; var ts = boogieType as TypeSynonymAnnotation; TypeConstructorInstance originalType; if (ts == null) { originalType = null; } else { TypeConstructor originalConstructor = makeConstructor(ts.Decl.Name, ts.Decl.TypeParameters.Count); originalType = new TypeConstructorInstance(originalConstructor, makeTypes(ts.Arguments, context)); } // string originalName = (ts!=null)?ts.ToString():null; if (boogieType.IsBool) { result = BooleanType.booleanType; } else if (boogieType.IsInt) { result = IntegerType.integerType; } else if (boogieType.IsBv) { result = BitVectorType.makeBitVectorType((boogieType as BvType).BvBits); } else if (boogieType.IsCtor) { TypeConstructor constructor = makeConstructor(boogieType.AsCtor.Decl.Name, boogieType.AsCtor.Decl.Arity); result = new TypeConstructorInstance(constructor, makeTypes(boogieType.AsCtor.Arguments, context)); } else if (boogieType.IsMap) { Microsoft.Boogie.MapType bmt = boogieType.AsMap; pushTypeVariables(ref context, bmt.TypeParameters); result = new MapType( makeTypeVariables(context, bmt.TypeParameters), makeTypes(bmt.Arguments, context), makeTypeI(bmt.Result, context), originalType); context.pop(); } else if (boogieType.IsVariable) { result = new VariableType(context.lookupTypeVariable(boogieType.AsVariable.Name)); } else { throw new Exception("Unsupported Boogie type:" + boogieType); } result.check(context); return(result); }
// http://stackoverflow.com/questions/8219343/reflection-emit-create-object-with-parameters public static ConstructorHandler CreateFactory(Type t, params object[] args) { TypeConstructor[] ctors; if (!dic.TryGetValue(t, out ctors)) { lock (Sync) { if (!dic.TryGetValue(t, out ctors)) { var list = new List <TypeConstructor>(); foreach (var each in t.GetConstructors(BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { var ps = each.GetParameters(); var item = new TypeConstructor(ps.Select(x => new ArgumentInfo(x.ParameterType)).ToArray()); var method = new DynamicMethod($"._{Guid.NewGuid()}", typeof(object), new Type[] { typeof(object[]) }, true); var il = method.GetILGenerator(); il.DeclareLocal(typeof(int)); il.DeclareLocal(typeof(object)); il.Emit(OpCodes.Ldc_I4_0); // [0] il.Emit(OpCodes.Stloc_0); //[nothing] var parameters = each.GetParameters(); for (int i = 0; i < parameters.Length; i++) { EmitInt32(il, i); // [index] il.Emit(OpCodes.Stloc_0); // [nothing] il.Emit(OpCodes.Ldarg_0); //[args] EmitInt32(il, i); // [args][index] il.Emit(OpCodes.Ldelem_Ref); // [item-in-args-at-index] var paramType = parameters[i].ParameterType; if (paramType != typeof(object)) { il.Emit(OpCodes.Unbox_Any, paramType); // same as a cast if ref-type } } il.Emit(OpCodes.Newobj, each); //[new-object] if (each.DeclaringType.IsValueType) { il.Emit(OpCodes.Box, each.DeclaringType); } il.Emit(OpCodes.Stloc_1); // nothing il.Emit(OpCodes.Ldloc_1); //[new-object] il.Emit(OpCodes.Ret); item.Constructor = (ConstructorHandler)method.CreateDelegate(typeof(ConstructorHandler)); list.Add(item); } dic[t] = list.ToArray(); } } } if (ctors == null) { ctors = dic[t]; } return(ctors?.FirstOrDefault(x => x.IsCompatibleParameters(args))?.Constructor); }
////////////////////////////////////////////////////////////////////////// private static string toBoogie(TypeConstructor typeConstructor) { string result = typeConstructor.name; for (int i = 0; i < typeConstructor.arity; i++) { result += " m" + i.ToString(); } return(result); }
public void OverriddenNameAndDescription() { var typeConstructor = new TypeConstructor(); var type = (ObjectGraphType)typeConstructor.Derive(Entity.New(typeof(Data.OverriddenNameAndDescription))); type.ShouldBeType <ObjectType <Data.OverriddenNameAndDescription> >(); type.Name.ShouldEqual("EmptyOverride"); type.Description.ShouldEqual("Lorem ipsum"); type.Interfaces.ShouldBeEmpty(); type.Fields.ShouldBeEmpty(); }
// private int freshProgramVariableIndex; // private int freshBoundVariableIndex; ////////////////////////////////////////////////////////// //types ////////////////////////////////////////////////////////// public void addTypeConstructor(string name, TypeConstructor typeConstructor) { if (parentScope != null) { parentScope.addTypeConstructor(name, typeConstructor); } else { typeConstructorMap[name] = typeConstructor; } }
public void Empty() { var typeConstructor = new TypeConstructor(); var type = (ObjectGraphType)typeConstructor.Derive(Entity.New(typeof(Data.Empty))); type.ShouldBeType <ObjectType <Data.Empty> >(); type.Name.ShouldEqual(nameof(Data.Empty)); type.Description.ShouldEqual(null); type.Interfaces.ShouldBeEmpty(); type.Fields.ShouldBeEmpty(); }
public override void DeriveMetaData(Entity entity) { if (TypeConstructor.IsType(entity)) { entity.WrappedType = (IWrappedType)Activator.CreateInstance(_wrapper, entity); } else { var resultEntity = entity.Construct(entity.OriginalType); entity.WrappedType = (IWrappedType)Activator.CreateInstance(_wrapper, resultEntity); } }
public static TInterface Resolve <TInterface>(TypeResolverContext context) { ITypeRegistration registration = context.RegisteredTypes.FirstOrDefault(tr => tr.InterfaceType == typeof(TInterface)); if (null == registration) { throw new InvalidOperationException("Unregistered interfaces cannot be resolved to a concrete type."); } TypeConstructor typeConstructor = ResolveDependencies(context, registration); return((TInterface)typeConstructor.Constructor.Invoke(typeConstructor.Parameters)); }
public void FieldTypes() { var typeConstructor = new TypeConstructor(); var type = (ObjectGraphType)typeConstructor.Derive(Entity.New(typeof(Data.Foo))); type.ShouldBeType <ObjectType <Data.Foo> >(); type.Interfaces.ShouldBeEmpty(); type.Fields.Count().ShouldEqual(3); var field = type.ShouldHaveField("field"); field.Name.ShouldEqual(NameAttribute.AsFieldName(nameof(Data.Foo.Field))); field.Description.ShouldEqual("Hello"); field.DeprecationReason.ShouldEqual("World"); field.Arguments.ShouldBeEmpty(); field.DefaultValue.ShouldBeNull(); field.Type.ShouldEqual(typeof(StringGraphType)); var property = type.ShouldHaveField("property"); property.Name.ShouldEqual(NameAttribute.AsFieldName(nameof(Data.Foo.Property))); property.Description.ShouldEqual(null); property.DeprecationReason.ShouldEqual(null); property.Arguments.ShouldBeEmpty(); property.DefaultValue.ShouldBeNull(); property.Type.ShouldEqual(typeof(NonNullGraphType <IntGraphType>)); var method = type.ShouldHaveField("method"); method.Name.ShouldEqual(NameAttribute.AsFieldName(nameof(Data.Foo.Method))); method.Description.ShouldEqual(null); method.DeprecationReason.ShouldEqual(null); method.Arguments.Count.ShouldEqual(2); method.DefaultValue.ShouldBeNull(); method.Type.ShouldEqual(typeof(BooleanGraphType)); var arg1 = method.Arguments[0]; arg1.Name.ShouldEqual("a"); arg1.Description.ShouldEqual("abc"); arg1.Type.ShouldEqual(typeof(IntGraphType)); arg1.DefaultValue.ShouldBeNull(); var arg2 = method.Arguments[1]; arg2.Name.ShouldEqual("b"); arg2.Description.ShouldEqual(null); arg2.Type.ShouldEqual(typeof(NonNullGraphType <FloatGraphType>)); arg1.DefaultValue.ShouldEqual(1.0f); }
public static void TestSet1_Method14() { //TestSet1 var typeConstructor = new TypeConstructor(); var typeFilter = new TypeFilter(); var typeParser = new TypeParser(); var typeResolver = new TypeResolver(); var container = new Container(new TypeActivator(), typeConstructor, typeFilter, typeParser, typeResolver); container.RegisterType <SqlDataRepository>(); container.RegisterType <ServiceDataRepository>(); var srv1 = container.CreateInstance <ServiceDataRepository>(); Assert.NotNull(srv1); }
/// <summary> /// Registers a class dependent type. /// </summary> /// <param name="identifier">The exact tag identifier of the type.</param> /// <param name="typeConstructor">The type constructor.</param> /// <param name="replace">True to allow replacing an existing registration, otherwise false</param> /// <returns>The <see cref="DefaultDerAsnDecoder"/> instance.</returns> public DefaultDerAsnDecoder RegisterType(DerAsnIdentifier identifier, TypeConstructor typeConstructor, bool replace = false) { if (identifier == null) { throw new ArgumentNullException(nameof(identifier)); } if (typeConstructor == null) { throw new ArgumentNullException(nameof(typeConstructor)); } if (!replace && _registeredClassSpecificTypes.ContainsKey(identifier)) { throw new InvalidOperationException($"Type with class {identifier.TagClass}, encoding type {identifier.EncodingType} and tag number {identifier.Tag} already registered"); } _registeredClassSpecificTypes[identifier] = typeConstructor; return(this); }
/// <summary> /// Registers a generic, class independent type. /// </summary> /// <param name="encodingType">The encoding type.</param> /// <param name="tag">The tag number.</param> /// <param name="typeConstructor">The type constructor.</param> /// <param name="replace">True to allow replacing an existing registration, otherwise false</param> /// <returns>The <see cref="DefaultDerAsnDecoder"/> instance.</returns> public DefaultDerAsnDecoder RegisterGenericType(DerAsnEncodingType encodingType, long tag, TypeConstructor typeConstructor, bool replace = false) { if (typeConstructor == null) { throw new ArgumentNullException(nameof(typeConstructor)); } var typeKey = new TypeKey { EncodingType = encodingType, Tag = tag }; if (!replace && _registeredTypes.ContainsKey(typeKey)) { throw new InvalidOperationException($"Type with encoding type {encodingType} and tag number {tag} already registered"); } _registeredTypes[typeKey] = typeConstructor; return(this); }
public void Tasks() { var typeConstructor = new TypeConstructor(); var type = (ObjectGraphType)typeConstructor.Derive(Entity.New(typeof(Data.Baz))); type.ShouldBeType <ObjectType <Data.Baz> >(); type.Interfaces.ShouldBeEmpty(); type.Fields.Count().ShouldEqual(1); var field = type.ShouldHaveField("findInt"); field.Name.ShouldEqual(NameAttribute.AsFieldName(nameof(Data.Baz.FindInt))); field.Type.ShouldEqual(typeof(NonNullGraphType <IntGraphType>)); field.Arguments.Count.ShouldEqual(1); var arg = field.Arguments[0]; arg.Name.ShouldEqual("input"); arg.Type.ShouldEqual(typeof(IntGraphType)); arg.DefaultValue.ShouldBeNull(); }
public InterfaceInstanceBuilder() { var dynamicTypeName = typeof(T).FullName + "_DynamicImplementation"; if (!DynamicTypeBuilder.Instance.TryGetRegisteredType(dynamicTypeName, out var typeToBuild)) { typeToBuild = DynamicTypeBuilder.Instance.CreateType(builder => { builder .HasName(dynamicTypeName) .ImplementsInterface(typeof(T)) .AddPropertiesFromType(typeof(T), config => config .AreInitializedInConstructorOptional() .HaveGetter(GetSetAccessModifier.Public) .HaveSetter(GetSetAccessModifier.Public)); }); } typeConstructor = new TypeConstructor <T>(typeToBuild); }
public void TypeConstructorTest() { var ctor1 = new TypeConstructor <Customer, int, string>(ProxyTypeBuilder.BuildDomainEntityProxy <Customer>(), (Customer x) => x.OnLoaded()); var id = 1; var name = "someone"; var c1 = ctor1.Construct(new object[] { id, name }); Assert.IsTrue(c1.Id == id && c1.Name == name && c1.GetEntityState() == EntityState.Existing); c1 = ctor1.Construct(("id", id), ("name", name)); Assert.IsTrue(c1.Id == id && c1.Name == name && c1.GetEntityState() == EntityState.Existing); c1 = ctor1.Construct(1, name); Assert.IsTrue(c1.Id == id && c1.Name == name && c1.GetEntityState() == EntityState.Existing); c1 = ctor1.ConstructWithDefaults(("id", id)); Assert.IsTrue(c1.Id == id && c1.Name == null && c1.GetEntityState() == EntityState.Existing); Assert.Throws <InvalidOperationException>(() => c1 = ctor1.Construct(("id", id))); }
public void TypeConstructorTest() { var id = 1; var name = "someone"; var code = "somecode"; var ctor1 = new TypeConstructor <Customer, int, string>(typeof(Customer1), initializer: x => x.Code = code); var c1 = ctor1.Construct(new object[] { id, name }); Assert.IsTrue(c1.Id == id && c1.Name == name && c1.Code == code); c1 = ctor1.Construct(("id", id), ("name", name)); Assert.IsTrue(c1.Id == id && c1.Name == name && c1.Code == code); c1 = ctor1.Construct(1, name); Assert.IsTrue(c1.Id == id && c1.Name == name && c1.Code == code); c1 = ctor1.ConstructWithDefaults(("id", id)); Assert.IsTrue(c1.Id == id && c1.Name == null && c1.Code == code); Assert.Throws <InvalidOperationException>(() => c1 = ctor1.Construct(("id", id))); }
public virtual void visitTypeConstructor(TypeConstructor typeConstructor) { }
////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// private void dumpTypeConstructor(TypeConstructor tc) { file.WriteLine("type {0};", toBoogie(tc)); }
protected CompositeType(string identifier, TypeConstructor typeConstructor, long scopeId) : base(identifier) { TypeConstructor = typeConstructor; ScopeId = scopeId; }
internal Entity() { Cache = new Dictionary <Tuple <string, bool>, Entity>(); TypeConstructor = new TypeConstructor(); }
public static void Register(Type type, TypeConstructor constructor) { s_Constructors[type] = constructor; }
public void FieldTypes() { var typeConstructor = new TypeConstructor(); var entity = Entity.New(typeof(Data.Foo)); var type = (ObjectGraphType)typeConstructor.Derive(entity); type.ShouldBeType <ObjectType <Data.Foo> >(); type.Interfaces.ShouldBeEmpty(); type.Fields.Count().ShouldEqual(3); var field = type.ShouldHaveField("field"); field.Name.ShouldEqual(NameAttribute.AsFieldName(nameof(Data.Foo.Field))); field.Description.ShouldEqual("Hello"); field.DeprecationReason.ShouldEqual("World"); field.Arguments.ShouldBeEmpty(); field.DefaultValue.ShouldBeNull(); field.Type.ShouldEqual(typeof(StringGraphType)); var property = type.ShouldHaveField("property"); property.Name.ShouldEqual(NameAttribute.AsFieldName(nameof(Data.Foo.Property))); property.Description.ShouldEqual(null); property.DeprecationReason.ShouldEqual(null); property.Arguments.ShouldBeEmpty(); property.DefaultValue.ShouldBeNull(); property.Type.ShouldEqual(typeof(NonNullGraphType <IntGraphType>)); var method = type.ShouldHaveField("method"); method.Name.ShouldEqual(NameAttribute.AsFieldName(nameof(Data.Foo.Method))); method.Description.ShouldEqual(null); method.DeprecationReason.ShouldEqual(null); method.Arguments.Count.ShouldEqual(2); method.DefaultValue.ShouldBeNull(); method.Type.ShouldEqual(typeof(NonNullGraphType <EnumType <Data.Baz> >)); var arg1 = method.Arguments[0]; arg1.Name.ShouldEqual("foo"); arg1.Description.ShouldEqual(null); arg1.Type.ShouldEqual(typeof(InputObjectType <Data.Foo>)); arg1.DefaultValue.ShouldBeNull(); var arg2 = method.Arguments[1]; arg2.Name.ShouldEqual("baz"); arg2.Description.ShouldEqual(null); arg2.Type.ShouldEqual(typeof(EnumType <Data.Baz>)); arg2.DefaultValue.ShouldBeNull(); var entityField = entity.Fields.First(f => f.Name == "method"); var entityArg1 = entityField.Arguments[0]; entityArg1.WrappedType.Entity.IsInput.ShouldBeTrue(); var entityArg2 = entityField.Arguments[1]; entityArg2.WrappedType.Entity.IsInput.ShouldBeTrue(); }