Search a CodePlex project

Project Description
a set of 'do as you expected' instead of 'do as you said' type casting functions to provide generic types casting functionality

this project is targeting to provide run-time casting in .net, which is mainly using in generic programming.
.net framework provides implicit and explicit casting support, but since CLI is working on type erasure (the template meta will be erased or replaced as the constraint when compile time). so it's impossible to cast between two unknown types. an example is as following function
bool Convert<T, T2>(List<T> l, out List<T2> r) { }
since there is no relationship between T and T2, .net framework does not allow to do the casting as
and it's impossible to write a function to convert a generic template to another even you have provided explicit / implicit conversion between the real types used during run-time.
so you properly need to write
bool Convert(List<int> l, out List<string> r) { }
bool Convert(List<int> l, out List<long> r) { }
bool Convert(List<Class1> l, out List<Class2> r) { }
but in fact the logic in these functions may almost be the same.

this project is targeting this kind of scenarios, instead of 'do what i say', it provides the convenience to 'do what i mean'. you can use the functions provided in the project to cast between any two types, no matter they are generic types or real types.

the casting is working in the following orders,
1. cast as nothing, if the source is null <nothing in>, will directly output a null. the valuetype / struct will not be impacted.
2. DirectCast, almost the same as 'as' in c#, if the source and target has inherit / implement relationship, will output the same instance as source
3. if on mono, try to use Convert.ChangeType. it's a bug in mono, which cause the trouble.
4. CType, almost the same as (T)(dynamic) in c#, it can handle some internal types as IConvertible.
5. run-time casting, it will try to find the implicit / explicit conversion functions provided in both input and output types, and run the cast function to do a run-time casting, and output a new instance.

so following scenarios are supported,
cast_case(Of Int32, Int32)(i32)
cast_case(Of Int32, Int64)(i32)
cast_case(Of Int32, String)(i32)
cast_case(Of Int32?, Int32)(New Int32?(i32))
cast_case(Of Int32?, Int64)(New Int32?(i32))
cast_case(Of Int32?, String)(New Int32?(i32))
cast_case(Of ctyped_class, inherit_class)(New ctyped_class())
cast_case(Of ctyped_class, base_class)(New ctyped_class())
cast_case(Of ctyped_class, return_s)(New ctyped_class())
'ctyped_class has implicit conversion function to inherit_class provided
'inherit_class is inherited from base_class
'base_class implemented return_s
'return_s is an interface
for the full supported scenarios, please refer to the test cases associated in the source code.
platform support, .net 2.0 or higher, mono is also supported, while it has been tested against mono 2.8 and 3.2.

due to the limitation of .net framework, the performance of a generic function is lower than a normal function, so to the known types, using the cast functions is also OK, but the performance will be lower than the internal casting methods.

p.s. really sorry, i do not want to rewrite everything in this project, so i just copy / paste the logic in the source file, but the test cases are not ported to here <it's really a big job to separate the unittest framework i am now using to an individual project>.
it's a very small piece of code in my main trunk, to find out the newest update and test cases, please refer to the the cast functions are in, while the test cases are in

feel free to drop me a message via [email protected] if you have any question about this project.
it's my pleasure if this can help you.

non-commercial use only, or please contact me if you need to use the code for commercial purpose
except for the companies i am now working or was working at.

Not affiliated with Microsoft and CodePlex
To remove your project or any question, please contact us: [email protected]