none
Inline get and set with CodeMemberProperty RRS feed

  • Question

  • Hi,
    The following code:

    CodeMemberProperty clrProperty = new CodeMemberProperty();
    clrProperty.Attributes = MemberAttributes.Public | MemberAttributes.Final;
    clrProperty.Name = property.Key;
    clrProperty.HasGet = true;
    clrProperty.HasSet = true;
    clrProperty.Type = new CodeTypeReference(typeof(DateTime));
    targetClass.Members.Add(clrProperty);

    generates this:

    public System.DateTime Start
    {
        get
        {
        }
        set
        {
        }
    }

    I would like to get the getter and setter inline:

    public System.DateTime Start
    {
        get;
        set;
    }

    What do I need to change to achieve my goal?

    Thanks


    • Edited by MsdnDev Thursday, January 3, 2013 9:06 AM
    Thursday, January 3, 2013 9:05 AM

Answers

  • I don't think that's possible as CodeMemberProperty does not seem to have any way to control that and I think it's because the inline is not the compiled result of the code.
    • Proposed as answer by Mike FengModerator Friday, January 4, 2013 7:09 AM
    • Marked as answer by MsdnDev Sunday, January 6, 2013 9:23 AM
    Thursday, January 3, 2013 2:01 PM

All replies

  • I don't think that's possible as CodeMemberProperty does not seem to have any way to control that and I think it's because the inline is not the compiled result of the code.
    • Proposed as answer by Mike FengModerator Friday, January 4, 2013 7:09 AM
    • Marked as answer by MsdnDev Sunday, January 6, 2013 9:23 AM
    Thursday, January 3, 2013 2:01 PM
  •            

    foreach(varpropertiy in_properties)

                {

                   

    varpropertyTypeName = CSharpStyleType.Convert(propertiy.PropertyType.Name);

                   

    varmemberProperty = newCodeMemberField(propertyTypeName+" ", propertiy.Name + " { get; set; }")

                    {

                         Attributes =

    MemberAttributes.Public | MemberAttributes.Final

                    };

                    wrapProxyClass.Members.Add(memberProperty);

                }

               

    varcprovider = newCSharpCodeProvider();

               

    varfileContent = newStringBuilder();

               

    using(varsw = newStringWriter(fileContent))

                {

                    cprovider.GenerateCodeFromCompileUnit(compunit, sw,

    newCodeGeneratorOptions{ BracingStyle ="C"});

                }

               

    varfi = newFileInfo(targetFile);

               

    if(fi.Directory != null&& !fi.Directory.Exists)

                {

                    fi.Directory.Create();

                }

                fileContent=fileContent.Replace(

    "{ get; set; };", "{ get; set; }");

     

               

    File.WriteAllText(targetFile, fileContent.ToString(), Encoding.UTF8);

    Tuesday, July 21, 2015 4:48 AM