`

c# - TypeCode to facilitate communication cross language boundary

    博客分类:
  • C#
c# 
阅读更多

Within the type system boundary of the .NET, it is fine that you directly use the System.Type namespace, and you can directly parse, pass, exchange values based on the type information that you gained at runtime. 

 

However, when it comes to cross language/machine boundaries, the Runtime Type System does not help much, suppose that you are exchange information with some server running on  Linux box writting in language such as c++, then there is you way that you can convert some machine representation from one box to another. 

 

the TypeCode enum is meant to provide a system/platform agnostic type system where you can exchange the messages and by parsing/passing/serializing the field of the of the primitive types (except for the Object TypeCode), you can achieve certain type of interopability.

 

 

 

Here is an example of showing you how to leverage the raw TypeCode system.

 

 

class Program
  {
    static void Main(string[] args)
    {
      WriteObjectInfo(new object());
      WriteObjectInfo(false);
      WriteObjectInfo(1.23);
      WriteObjectInfo("hello world");

      var @string = ConvertTypeCode("hello world", TypeCode.String);
      Console.WriteLine("String: {0}", @string);

      var @double = ConvertTypeCode(1.23, TypeCode.Double);
      Console.WriteLine("double: {0}", @double);

      try
      {
        @double = ConvertTypeCode(1.23, TypeCode.String);

        Console.WriteLine("double -> string: {0}", @double);
      }
      catch (Exception ex_)
      {
        Console.WriteLine("ex_: {0}", ex_.ToString());
      }
      
      try
      {
        @double = ConvertTypeCode("1.23", TypeCode.Double);
        Console.WriteLine("string -> double : {0}", @double);
      }
      catch (Exception ex_)
      {
        Console.WriteLine("ex_: {0}", ex_.ToString());
      }

      try
      {
        @double = ConvertTypeCode("abcd", TypeCode.Double);
        Console.WriteLine("string -> double : {0}", @double);
      }
      catch (Exception ex_)
      {
        Console.WriteLine("ex_: {0}", ex_.ToString());
      }
    }


    static void WriteObjectInfo(object testObject)
    {
      TypeCode typeCode = Type.GetTypeCode(testObject.GetType());
      switch (typeCode)
      {
        case TypeCode.Boolean:
          Console.WriteLine("Boolean: {0}", testObject);
          break;
        case TypeCode.Double:
          Console.WriteLine("Double : {0}", testObject);
          break;
        default:
          Console.WriteLine("{0}: {1}", typeCode.ToString(), testObject);
          break;
      }
    }


    static object ConvertTypeCode(object value, TypeCode typeCode)
    {
      return Convert.ChangeType(value, typeCode);
    }
  }
 

As you can see, you can get a TypeCode based on the runtime type information. Convert provide a ChangeType function you can assign a value in object to a value of type represented by the TypeCode.

 

 

You might get some format exception/other exception unknown if you try to convert imcompatible types.

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics