c# Generic type and real type associations -


i´m building communications system using tcp/ip. i´m using .net, c# , vs2012.

those systems exchange metada between them. basic message contains metadata name , it´s value (the data itself). 1 system doesn´t know other system datatypes.

the message has following struct: fromnode, tonode, metadataname, metadatatype, metadatasizeinbytes, metadatavalue. messages pure byte[] delimited fields (not serialized, systems can unix flavoured).

when receiving message associate metadata real variable, based on type. built following class:

public class realmetadata

{          public string type { get; set; }          public type   value { get; set; } <<--- i´ve tried var , object, no success. } 

later on code wanna access real value of metadata:

if (type == "integer")       int realvalue = (int) value; if (type == "string")       string realvalue;       buffer.blockcopy (realvalue, 0, value, 0, metadatasizeinvalues * sizeof (char)); if (type == "float")       float realvalue = (float) value; 

this whole thing don´t compile or doesn´t work @ runtime. don´t know type value shall declared on class property, , how data real variable.

thanks lot idea or suggestion.

rds

you should using object hold data , type object hold type of data stored, bellow:

    public type type { get; set; }     public object value { get; set; }           if (type == typeof (int))         {             int realvalue = (int) value;         }         if (type == typeof(string))         {             string realvalue;             buffer.blockcopy(realvalue, 0, value, 0, metadatasizeinvalues*sizeof (char));         }         if (type == typeof(float))         {             float realvalue = (float) value;         } 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -