Archived

This forum has been archived. Please start a new discussion on GitHub.

C# classes

Does anyone have an interest in having the ICE class's data members be generated in C# as properties instead of public fields? The advantage is is that you can drop a class object with properties into a property grid & easily view the data. This make debugging [realtime programs] easier. It would be a "nice to have" to have ZeroC implement this as an option [for C# & VB]

Comments

  • I guess we could make this available as a metadata directive. I considered mapping to properties at one point, but couldn't see any advantage for a type whose code is generated.

    Cheers,

    Michi.
  • C# properties instead of public fields

    Having C# public properties instead of public fields is something that we can really benefit from, so I took a stab at it. below is the replacement VisitDataMember in "slice2cs\Gen.cpp file. It seems to work just fine, & the default functionality remains the same. Could this be added to your code baseline ? vb.net would be easy to do also.

    THANKS
    Scott Emerich
    jscott.emerich@boeing.com


    Slice::Gen::TypesVisitor::visitDataMember(const DataMemberPtr& p)
    {
    int baseTypes = 0;
    bool isClass = false;
    ContainedPtr cont = ContainedPtr::dynamicCast(p->container());
    assert(cont);
    if(StructPtr::dynamicCast(cont) && cont->hasMetaData("clr:class"))
    {
    baseTypes = DotNet::ICloneable;
    }
    else if(ExceptionPtr::dynamicCast(cont))
    {
    baseTypes = DotNet::ApplicationException;
    }
    else if(ClassDefPtr::dynamicCast(cont))
    {
    baseTypes = DotNet::ICloneable;
    isClass = true;
    }
    _out << sp << nl;

    string nameS = fixId(p->name(), baseTypes, isClass);
    string ty = typeToString(p->type());
    if (p->hasMetaData("clr:property") || cont->hasMetaData("clr:property"))
    {
    _out << "private " << ty << " _" << nameS << ";" << nl;
    emitAttributes(p);
    _out << "public " << ty << " " << nameS << nl;
    _out << "{ get { return _" << nameS << "; }" << nl;
    _out << " set { _" << nameS << " = value; } }" << nl;
    }
    else
    {
    emitAttributes(p);
    _out << "public " << ty << " " << nameS << ";";
    }
    }
  • Thanks for that! I'll add something like this for the next release.

    Cheers,

    Michi.
  • properties (getter / setter) of slice2cs generated classes?
    michi wrote: »
    Thanks for that! I'll add something like this for the next release.

    Cheers,

    Michi.


    Hi Michi,

    Was this ever added? I'm evaluating to use Ice here (version 3.5.0), but would be keen to get get; set; properties to be generated by the slice2cs compiler (as I want to keep the slice generated classes in some WPF projects to display them in things like a listbox (don't work with the slice2cs generated classes, but do work if I manually convert those classes to use public properties instead of fields).

    Thanks,

    filgood
  • xdm
    xdm La Coruña, Spain
    Hi Filip,

    You can map attributes to properties using the "clr:property" metadata

    see C-Sharp Mapping for Classes - Ice 3.5 - ZeroC

    Regards,
    Jose
  • Many thanks! That solved it.

    ~ filgood