如何使用CodeDom实现非对称访问器可访问性?

时间:2013-03-21 03:54:32

标签: c# codedom

如何使用CodeDOM;

实现Asymmetric Accessor Accessibility
public string Name
{
    get
    {
        return name;
    }
    protected set
    {
        name = value;
    }
}

我发现了一个重复的问题here但是,现在运行中的C#5.0和.NET framework 4.5。现在也不可能吗?

通过此代码;

//Declare a property in target class and set attributes of the property
                CodeMemberProperty newPropertyGetter = new CodeMemberProperty();
                newPropertyGetter.Attributes = (MemberAttributes)Enum.Parse(typeof(MemberAttributes), visibilityGetter) | MemberAttributes.Final;
                newPropertyGetter.Name = propName;
                newPropertyGetter.Type = new CodeTypeReference(propType);
                newPropertyGetter.HasGet = needGet;


                CodeMemberProperty newPropertySetter = new CodeMemberProperty();
                newPropertySetter.Attributes = (MemberAttributes)Enum.Parse(typeof(MemberAttributes), visibilitySetter) | MemberAttributes.Final;
                newPropertySetter.Name = propName;
                newPropertySetter.Type = new CodeTypeReference(propType);
                newPropertySetter.HasSet = needSet;

我可以这样;

public string Name
    {
        get
        {
            return name;
        }        
    }

public string Name
    {        
        protected set
        {
            name = value;
        }
    }

有效吗?或者这不是一个好习惯吗?

0 个答案:

没有答案