Since you have a restriction that the type you are converting implements an IConvertible interface (whereas the Guid structure ), you have no choice to create an overload, for example:
public static Guid ToType(this object val, Guid alt) { try {
When you pass Guid , it will be resolved due to section 7.4.2 of the C # specification (my attention):
Once the candidate function members and the argument list have been identified, selecting the best member of the function in all cases:
- Given the set of applicable candidate candidate members, the best member of the function in this set is found.
Given that Guid is a more specific match than a parameter of type T , the second method is called.
Please note that if you removed the IConvertible interface IConvertible , you could handle it in one method, but you would need to have logic to handle any structure that is passed to T (a TypeConverter would be convenient here).
source share