I've seen MSDN tutorial about implicit and explicit operators. I've made some tests and it works very well.
I know the difference between them (syntaxily speaking), but I do not know the best way to use them.
My question could have been : "What kind of convertions must be done using a cast?"
For now, I just haven't overriden implicit when the result can be null..
Example:
struct TypeRef
{
// If TypeRef is constructed with a type, will reference the given type.
// If TypeRef is constructed with a string, will be the result of 'Type.GetType(theString)' (if the type doesn't exist: null)
public readonly Type TypeIfExists;
// If TypeRef is constructed with a type, will be the result of theType.Name
// If TypeRef is constructed with a string, will reference this string.
public readonly string TypeName;
// Can be constructed by a string or a Type.
// Can be converted from a string or a Type implicitely and explicitely
// Can be converted to a string implicitely and explicitely
// Can be converted to a Type explicitely only
}
TypeRef tr1 = "SomeType";
TypeRef tr2 = typeof(SomeType);
TypeRef tr3 = "ThisTypeDoesntExist";
Type t1 = (Type)tr1;
Type t2 = (Type)tr3; // Will be null.
string s1 = (string)tr1;
string s2 = tr1;
Thank you :)
Edit: Asked the the question more clearly :p