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)); }
private TypeDef ImplementToString(TypeDef declaringType) { var properties = declaringType.Members .OfType <PropertyDef>() .Select(p => p.Signature) .Where(p => !p.IsStatic); var toStringSgn = MethodSignature.Override(declaringType.Signature, MethodSignature.Object_ToString); var toStringDef = MethodDef.Create(toStringSgn, @this => { Expression formatProperty(PropertySignature property) { return(@this.Read().ReadProperty(property)); } IEnumerable <Expression> stringFragments() { yield return(Expression.Constant(declaringType.Signature.Name)); yield return(Expression.Constant(" {")); var first = true; foreach (var property in properties) { if (first) { first = false; } else { yield return(Expression.Constant(",")); } yield return(Expression.Constant(" " + property.Name + " = ")); yield return(formatProperty(property)); } yield return(Expression.Constant(" }")); } return(ExpressionFactory.String_Concat(stringFragments())); }); return(declaringType.AddMember(toStringDef)); }