Archived

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

C#: Attributes with Ice generated classes and structs

hi,

i'd like to use .net serialization on ice generated classes/structs.
therefore i need set the attribute [Serializable] in the beginning of the
call definition.

is there a way to do this?
maybe the compiler could support a metadata directive for this.

thx tom

PS: another solution would be to work with ice classes and registed and
object factory which generates an object base on a class with the attribute.
but thats a bit of an overkill fo just have one attribues set.

PPS: such a metadata directive is also needed for sequences and
dictionaries - in case ZeroC is planning to add this feature ;-)

Comments

  • hi everybody,

    i allowed myself to modify the slice2sc sources in order to support
    a new meta data directive:

    ["cs:attribute(*)"]
    where * is the attribute added to the generated csharp source

    example:
    module A
    {
    	module B
    	{
    		["cs:attribute(System.Serializable)", 
    		"cs:attribute(System.ComponentModel.TypeConverter(typeof(MyClassConverter)))"]
    		struct S
    		{
    			int i0;
    		};
    	};
    };
    

    will create following code:
    ...
    namespace A
    {
        namespace B
        {
    	[ System.ComponentModel.TypeConverter(typeof(MyClassConverter)) ]
    
    	[ System.Serializable ]
    
    	public struct S
    	{
    ...
    

    my changes in compiler source code is only for attribute usage on structs.
    changed files are slice/CsUtil.cpp and slice2cs/Gen.cpp.

    added lines in slice/CsUtil.cpp line number 1106
    			static const string meta_attribute = "attribute";
    
    			string tag = s.substr(prefix.size(), meta_attribute.size());
    			if( tag == meta_attribute )
    			{
    				continue;
    			}
    


    added lines in Gen.cpp line number 2098:
    	StringList metadata = p->getMetaData();
    	StringList::const_iterator iter = metadata.begin();
    	static const string meta_attribute = "cs:attribute";
    	for( ; iter != metadata.end(); ++iter )
    	{
    		string tag = iter->substr( 0, meta_attribute.size() );
    		if( tag != meta_attribute )
    			continue;
    
    		string::size_type idx0 = iter->find_first_of( '(' );
    		string::size_type idx1 = iter->find_last_of( ')' );
    		string::size_type count = idx1 - idx0 - 1;
    		string attribute = iter->substr( idx0 + 1, count );
    
    		_out << sp << nl << "[ " << attribute << " ]";
    	}
    

    in order to make the generated sources compile the attributes need be used with their full name.

    quick comment for the ZeroC team:
    if you want to incorporate this mechanism/support into the next versions,
    please let me know and i'll implement this on other code elements like classes, data members of structs etc.


    Take care and have a nice weekend

    tom
  • Thanks for that Tom!

    I will implement a mechanism such as this for the next major release. I'm not quite sure exactly what shape it will take, but it will apply not just to structs. I'd rather have a more general mechanism that applies to any Slice construct.

    BTW, the "cs" and "vb" metadata directives will be renamed to "clr", so if you set an attribute, it applies to both C# and VB. (The old directives will be deprecated and continue to work a few more releases.) It doesn't make sense to, for example, generate the array mapping for sequences in C#, but the collection mapping for sequences in VB because that makes the generated assemblies incompatible across the two languages.

    Cheers,

    Michi.