Class reflection in .NET creates methods that differ only in the modifier

I'm a little overwhelmed by something, hope the CLR gearboxes can help. My mechanisms seem to be small.

I have a reflector utility that generates assembly nodes for Cola for .NET, and I find that classes have methods that differ only in a modifier such as virtual. Example from Oracle.DataAccess.dll, GetType () method:

class OracleTypeException : System.SystemException { virtual string ToString (); virtual System.Exception GetBaseException (); virtual void set_Source (string value); virtual void GetObjectData (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context); virtual System.Type GetType (); // DeclaringType Exception virtual bool Equals (object obj); virtual int32 GetHashCode (); System.Type GetType (); // DeclaringType Object } 

What is it?

I was not able to reproduce this using C #, and this causes problems for Cola, since it considers GetType () to be an override, since the signature is identical.

My method reflector begins as follows:

 static void DisplayMethod(MethodInfo m) { if ( // Filter out things Cola cannot yet import, like generics, pointers, etc. m.IsGenericMethodDefinition || m.ContainsGenericParameters || m.ReturnType.IsGenericType || !m.ReturnType.IsPublic || m.ReturnType.IsPointer || m.ReturnType.IsByRef || m.ReturnType.IsMarshalByRef || m.ReturnType.IsImport ) return; // generate stub signature // [snipped] } 

SOLVE: Non-virtual GetType () comes from System.Object. The output class obscures System.Object.GetType () using a virtual method.

+4
source share
3 answers

It is possible. Here is a piece of code that compiles just fine:

 public class OracleTypeException : SystemException, _Exception { public virtual Type GetType() { throw new NotImplementedException(); } Type _Exception.GetType() { throw new NotImplementedException(); } } 

It relies on SystemException to implement _Exception , which the GetType method defines . So we have:

By the way, the compiler generates a warning about this.

+3
source

I would check:

  • Static?
  • Is this an implicit interface implementation?

Note that you can also re-declare methods that may be a factor; but that would be insanity; however, this shows the following:

 class Bar { new Type GetType() { return null; } } static class Program { static void Main() { var methods = typeof(Bar).GetMethods( BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); foreach (var method in methods) { Console.WriteLine(method.Name); } } } 
+3
source

This class should exhibit the same behavior:

  class Something { public virtual Type GetType() { throw new NotImplementedException(); } } 

Since each class inherits from System.Object, it also inherits from the non-virtual Object.GetType () method. Reusing the method as a virtual one (note that you don't even need a β€œnew” keyword) hides the original inherited GetType method.

Not sure what this tool requires, but I suppose you need to rename the hidden inherited method with something like Object_GetType.

+1
source

Source: https://habr.com/ru/post/1307236/


All Articles