TypeDef MyStructWithMethod() { var thisP = ParameterExpression.CreateThisParam(MyStruct); var method = MethodDef.Create(MyStruct_Method, _ => Expression.Nop); return(MyStructDef.AddMember(method)); }
public void NameSanitization() { var stringT = TypeReference.FromType(typeof(string)); var type = TypeSignature.Class("MyType", ns, Accessibility.APublic); cx.AddType(TypeDef.Empty(type).AddMember( // Should be renamed, there is collision with virtual object.Equals new FieldDef(new FieldSignature(type, "Equals", Accessibility.APublic, stringT, false, true)) )); var type2 = TypeSignature.Class("MyType2", ns, Accessibility.APublic); cx.AddType(TypeDef.Empty(type2).AddMember( // OK, no collision here new MethodDef( MethodSignature.Instance("Equals", type2, Accessibility.APublic, TypeSignature.Boolean), ImmutableArray.Create(ParameterExpression.CreateThisParam(type2)), Expression.Constant(true) ) )); var type3 = TypeSignature.Class("MyType3", ns, Accessibility.APublic); cx.AddType(TypeDef.Empty(type3).AddMember( // Should be renamed new MethodDef( MethodSignature.Instance("Equals", type3, Accessibility.APublic, TypeSignature.Boolean, new MethodParameter(TypeSignature.Object, "obj2")), ImmutableArray.Create(ParameterExpression.CreateThisParam(type3), ParameterExpression.Create(TypeSignature.Object, "obj2")), Expression.Constant(true) ) )); var type4 = TypeSignature.Class("MyType4", ns, Accessibility.APublic); cx.AddType(TypeDef.Empty(type4).AddMember( // Should be renamed new MethodDef( MethodSignature.Static("Equals", type4, Accessibility.APublic, TypeSignature.Boolean, new MethodParameter(TypeSignature.Object, "obj2")), ImmutableArray.Create(ParameterExpression.Create(TypeSignature.Object, "obj2")), Expression.Constant(true) ) )); var type5 = TypeSignature.Class("MyType5", ns, Accessibility.APublic); cx.AddType(TypeDef.Empty(type5).AddMember( // OK, this is override new MethodDef( MethodSignature.Instance("Equals", type5, Accessibility.APublic, TypeSignature.Boolean, new MethodParameter(TypeSignature.Object, "obj2")).With(isOverride: true, isVirtual: true), ImmutableArray.Create(ParameterExpression.CreateThisParam(type5), ParameterExpression.Create(TypeSignature.Object, "obj2")), Expression.Constant(true) ) )); check.CheckOutput(cx); }
public void StructInterface() { var icloneable = TypeSignature.FromType(typeof(ICloneable)); var method = new MethodSignature(MyStruct, ImmutableArray <MethodParameter> .Empty, "Clone", TypeSignature.Object, false, Accessibility.APublic, false, false, false, false, ImmutableArray <GenericParameter> .Empty); var thisP = ParameterExpression.CreateThisParam(method); var methodDef = new MethodDef(method, ImmutableArray.Create(thisP), Expression.ReferenceConversion(thisP, TypeSignature.Object)); cx.AddType(MyStructDef.AddMember(methodDef).AddImplements(new SpecializedType(icloneable, ImmutableArray <TypeReference> .Empty))); var interfaceClone = cx.GetMemberMethods(icloneable.NotGeneric()).Single(m => m.Signature.Name == "Clone"); var p = ParameterExpression.Create(MyStruct, "p"); cx.AddTestExpr(Expression.MethodCall(method, ImmutableArray <Expression> .Empty, p), p); cx.AddTestExpr(Expression.MethodCall(interfaceClone, ImmutableArray <Expression> .Empty, Expression.ReferenceConversion(p, icloneable)), p); check.CheckOutput(cx); }
TypeDef MyStructWithConstructor() { var methodSignature = MethodSignature.Constructor(MyStruct, Accessibility.APublic, new MethodParameter(GuidType, "id"), new MethodParameter(TypeSignature.Int32, "count")); var thisP = ParameterExpression.CreateThisParam(MyStruct); var idP = ParameterExpression.Create(GuidType, "id"); var countP = ParameterExpression.Create(TypeSignature.Int32, "count"); var method = MethodDef.Create(methodSignature, (thisP, idP, countP) => Expression.Block( ImmutableArray.Create( Expression.FieldAccess(MyStruct_GuidField, thisP).ReferenceAssign(idP), Expression.FieldAccess(MyStruct_IntField, thisP).ReferenceAssign(countP) ), result: Expression.Nop ) ); return(MyStructDef.AddMember(method)); }