c# - TypeCode to facilitate communication cross language boundary

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.

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值