Class VirtualMethod
A utility for keeping backwards compatibility on previously abstract methods (or similar replacements).
Before the replacement method can be made abstract, the old method must kept deprecated. If somebody still overrides the deprecated method in a non-sealed class, you must keep track, of this and maybe delegate to the old method in the subclass. The cost of reflection is minimized by the following usage of this class:
Define static readonly fields in the base class (BaseClass
),
where the old and new method are declared:
internal static readonly VirtualMethod newMethod =
new VirtualMethod(typeof(BaseClass), "newName", parameters...);
internal static readonly VirtualMethod oldMethod =
new VirtualMethod(typeof(BaseClass), "oldName", parameters...);
this enforces the singleton status of these objects, as the maintenance of the cache would be too costly else.
If you try to create a second instance of for the same method/baseClass
combination, an exception is thrown.
To detect if e.g. the old method was overridden by a more far subclass on the inheritance path to the current instance's class, use a non-static field:
bool isDeprecatedMethodOverridden =
oldMethod.GetImplementationDistance(this.GetType()) > newMethod.GetImplementationDistance(this.GetType());
// alternatively (more readable):
bool isDeprecatedMethodOverridden =
VirtualMethod.CompareImplementationDistance(this.GetType(), oldMethod, newMethod) > 0
GetImplementationDistance(Type) returns the distance of the subclass that overrides this method. The one with the larger distance should be used preferable. this way also more complicated method rename scenarios can be handled (think of 2.9 TokenStream deprecations).
Inheritance
Inherited Members
Namespace: Lucene.Net.Util
Assembly: Lucene.Net.dll
Syntax
public sealed class VirtualMethod
Constructors
| Improve this Doc View SourceVirtualMethod(Type, String, Type[])
Creates a new instance for the given baseClass
and method declaration.
Declaration
public VirtualMethod(Type baseClass, string method, params Type[] parameters)
Parameters
Type | Name | Description |
---|---|---|
System.Type | baseClass | |
System.String | method | |
System.Type[] | parameters |
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | if you create a second instance of the same
|
System.ArgumentException | If |
Methods
| Improve this Doc View SourceCompareImplementationDistance(Type, VirtualMethod, VirtualMethod)
Utility method that compares the implementation/override distance of two methods.
Declaration
public static int CompareImplementationDistance(Type clazz, VirtualMethod m1, VirtualMethod m2)
Parameters
Type | Name | Description |
---|---|---|
System.Type | clazz | |
VirtualMethod | m1 | |
VirtualMethod | m2 |
Returns
Type | Description |
---|---|
System.Int32 |
|
GetImplementationDistance(Type)
Returns the distance from the baseClass
in which this method is overridden/implemented
in the inheritance path between baseClass
and the given subclass subclazz
.
Declaration
public int GetImplementationDistance(Type subclazz)
Parameters
Type | Name | Description |
---|---|---|
System.Type | subclazz |
Returns
Type | Description |
---|---|
System.Int32 | 0 if and only if not overridden, else the distance to the base class. |
IsOverriddenAsOf(Type)
Returns, if this method is overridden/implemented in the inheritance path between
baseClass
and the given subclass subclazz
.
You can use this method to detect if a method that should normally be final was overridden by the given instance's class.
Declaration
public bool IsOverriddenAsOf(Type subclazz)
Parameters
Type | Name | Description |
---|---|---|
System.Type | subclazz |
Returns
Type | Description |
---|---|
System.Boolean |
|