- 浏览: 399573 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
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.
发表评论
-
wpf - example to enhance ComboBox for AutoComplete
2014-09-19 15:56 1976first let’s see an example ... -
Investigate and troubleshoot possible memory leak issue of .NET application
2014-07-31 10:42 0Hi All, I would like to sh ... -
C# – CoerceValueCallback合并、替换元数据值
2013-08-05 21:59 1925Topic: C# – CoerceValueCallbac ... -
wpf – ListView交替背景色
2013-07-02 20:56 6551Wpf – Alternate background col ... -
C# - 简单介绍TaskScheduler
2013-06-29 17:18 12038标题: C# - 简单介绍TaskSchedulerTit ... -
c# - Get enum from enum attribute
2013-06-27 21:32 1244DescriptionAttribute gives the ... -
C# - PInvoke, gotchas on the RegisterClassEx and the CreateWindowEx
2013-06-24 13:49 2571I get an exception message li ... -
c# - Use PInvoke to create simple win32 Application
2013-06-24 11:59 10946In this post, .net platform h ... -
c# - Linq's Select method as the Map function
2013-06-19 18:47 1287If you comes from a functiona ... -
c# - Tips of Linq expression Any to determine if a collection is Empty
2013-06-19 18:29 938When you are @ the linq expres ... -
myth buster - typeof accepting array of types not acceptable
2013-06-19 17:17 813I have seen from some book whe ... -
windows - trying to create WIN32 application with PInvoke
2013-06-19 14:34 0While it is stupid to do such ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1918You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5330As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1185Recently I was trying to debug ... -
c# - P/Invoke, DllImport, Marshal Structures and Type conversions
2013-06-05 15:25 1712P/Invoke as in the following q ... -
c# - A study on the NativeWindow - encapsulate window handle and procedure
2013-06-05 14:40 6091NativeWindow gives you a way t ... -
WCF - Notify server when client connects
2013-06-03 18:19 1220It is sometimes very importan ... -
wcf - Debug to enable Server exception in Fault message
2013-06-03 15:47 1091WCF will be able to send back ... -
c# - determine if a type/object is serialzable
2013-05-30 16:35 867In WCF, primitives type are s ...
相关推荐
Organizational consultation to facilitate communication within a school staff ORGANIZATIONAL CONSULTATION TO FACILITATE COMMUNICATION WITHIN A SCHOOL STAFF NAOMI LENNOX, DENNIS FLANAGAN, AND JOEL ...
(Demultiplex) modes,The GS12070 is highly configurable UHD-SDI Gearbox which performs multiplexing and de-multiplexing necessary to facilitate conversions between SMPTE ST 425-3 and/or ST 425-5 (multi...
- **Generics**: Introduced in C# 2.0, generics allow developers to write type-safe code that can work with any type. - **Creating Custom Generic Types and Methods**: Shows how to create generic ...
The IRNSS Signal-in-Space ICD for SPS is released to the public to provide the essential information on the IRNSS signal-in-space, to facilitate research
With its support for dynamic programming, C# 4.0 continues to evolve as a versatile language on its own. But when C# is used with .NET Framework 4, the combination is incredibly powerful. This best...
The Blood Bank Management System aims to facilitate the crucial task of managing blood donations, inventory, and requests within a blood bank organization.zip
classroom, this book uses real-world code to solve real-world problems with real-world solutions. The book starts by exploring the various means of installing MySQL for Python on different platforms ...
parametric-activation functions to facilitate the morphing of any continuous non-linear activation neurons. Experimental results on benchmark datasets and typical neural networks demonstrate the ...
Generate a two-dimensional code, to facilitate the transfer of information to mobile devices 二维码插件1. 用于将电脑上的文字内容生成二维码,通过手机的二维码扫描工具即传到手机上;2. 生成通讯录二维码并...
This is a Matlab library of functions to facilitate the design of Photonics Integrated Circuits GDS layout.
Device-to-device (D2D) communication that enables direct communication between nearby mobiles is an exciting and innovative feature of next-generation cellular networks. It will facilitate the ...
天韵国际控股是一家成立于2003年并在2015年于香港交易所上市的公司,专注于加工水果产品,不仅在中国国内销售,还为海外客户提供OEM服务,其中包括知名的国际品牌如Del Monte和Tesco等。公司近年来积极推广自有品牌...
This document describes relevant characteristics of the Landsat 8 Surface Reflectance(SR), Top of Atmosphere (TOA) Reflectance, and TOA Brightness Temperature (BT)products to facilitate their use in ...
* Set up App-V Management and Streaming Servers to facilitate the centralized management of applications. * Install App-V Sequencer, Desktop Client, and Terminal Services Client to set up the entire...
It explains how the protocol stack interacts with different layers to facilitate efficient power transfer and charging. Samsung's Power Delivery Extensions include an Enhanced Power Profile, which ...
matlab_对振动信号进行处理,提高信噪比,便于监测 The vibration signal is processed to improve the signal-to-noise ratio and facilitate monitoring.