How to implement operator 'is' with a type, that I got from reflection (since that type marked as internal)? My code:
Type someType = someObj.GetType();
Type runtimeType = Assembly.GetAssembly(typeof(string)).GetType("System.RuntimeType"); //Marked as internal
if (someType is runtimeType) //Not compiling...
{
//To do something
}
I wrote some hackery code, but may be there is a better solution than that:
private static bool _isThat<T, K>(T obj) { return obj is K; }
private static readonly MethodInfo _isThatMethodInfo = typeof(Program).GetMethod("_isThat",
BindingFlags.NonPublic | BindingFlags.Static);
public static bool IsThatType<T>(T obj, Type type)
{
return type == null || obj == null ? false
: (bool)_isThatMethodInfo.MakeGenericMethod(typeof(T), type).Invoke(null, new object[] { obj });
}
Unfortunately, method IsSubclassOf returns false if someType == runtimeType.
Aucun commentaire:
Enregistrer un commentaire