How to check if implicit or explicit cast exists?
I have a generic class and I want to enforce that instances of the type parameter are always "cast-able" / convertible from String. Is it possible to do this without for example using an interface?
Possible implementation:
public class MyClass<T> where T : IConvertibleFrom<string>, new()
{
public T DoSomethingWith(string s)
{
// ...
}
}
Ideal implementation:
public class MyClass<T>
{
public T DoSomethingWith(s开发者_开发百科tring s)
{
// CanBeConvertedFrom would return true if explicit or implicit cast exists
if(!typeof(T).CanBeConvertedFrom(typeof(String))
{
throw new Exception();
}
// ...
}
}
The reason why I would prefer this "ideal" implementation is mainly in order not to force all the Ts to implement IConvertibleFrom<>.
Given that you want to convert from the sealed String type, you can ignore possible nullable, boxing, reference and explicit conversions. Only op_Implicit()
qualifies. A more generic approach is provided by the System.Linq.Expressions.Expression class:
using System.Linq.Expressions;
...
public static T DoSomethingWith(string s)
{
var expr = Expression.Constant(s);
var convert = Expression.Convert(expr, typeof(T));
return (T)convert.Method.Invoke(null, new object[] { s });
}
Beware the cost of Reflection.
Why can't you do something like this?
public class MyClass<T> where T : string
{
public T DoSomethingWith(string s)
{
// ...
}
}
In this way you can check whether s
is convertible to T
in the DoSomethingWith
code. From there you can throw exception if it can't, or do the casting, if it can
if(!typeof(T).IsAssignableFrom(typeof(String))) {
throw new Exception();
}
精彩评论