[英]How to add a property to an existing class programmatically?
我想添加另一个属性public int ID { get; set; }
public int ID { get; set; }
public int ID { get; set; }
使用 C# 到下面的 class。 不知道T4旁边有没有办法或者部分class。 CodeDom 怎么样? 我不熟悉它。 请帮我一个忙,谢谢!
public class DgItem
{
public string Name { get; set; }
}
您为您的财产创建一个字段
CodeMemberField cmf = new CodeMemberField(new CodeTypeReference(type), name);
cmf.Attributes = attr;
cmf.InitExpression = new CodePrimitiveExpression(value);
你创建你的财产
CodeMemberProperty cmp = new CodeMemberProperty()
{
Name = name,
Type = new CodeTypeReference(type),
Attributes = attr,
};
然后添加 get 和 set 语句
cmp.GetStatements.Add(new CodeMethodReturnStatement(new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), field.Name)));
cmp.SetStatements.Add(new CodeAssignStatement(new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), field.Name), new CodePropertySetValueReferenceExpression()));
这应该可以解决问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.