I am translating from Java to C# and have code similar to:
Class<?> refClass = refChildNode.getClass();
Class<?> testClass = testChildNode.getClass();
if (!refClass.equals(testClass)) {
// Do something
}
and elsewhere use Class.isAssignableFrom(Class c)
... and similar methods
Is there a table of direct equivalents for class comparsion and properties and code-arounds where this isn't possible?
(The <?>
is simply to stop warnings from the IDE about generics. A better solution would be appreciated)
Type refClass = refChildNode.GetType();
Type testClass = testChildNode.GetType();
if (!refClass.Equals(testClass))
{
....
}
Have a look on System.Type class. It have methods you need.
Firstly, to get the class (or in .NET speak, Type) you can use the following method:
Type t = refChildNode.GetType();
Now you have the Type, you can check equality or inheritance. Here is some sample code:
public class A {}
public class B : A {}
public static void Main()
{
Console.WriteLine(typeof(A) == typeof(B)); // false
Console.WriteLine(typeof(A).IsAssignableFrom(typeof(B))); // true
Console.WriteLine(typeof(B).IsSubclassOf(typeof(A))); // true
}
This uses the System.Reflection functionality. The full list of available methods is here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With