Excluding an aspect from specific implementations of a generic class? - c#

Say I have a generic class:
public abstract class MyClass<T> {
// Contents
}
How can I specify an exclude for my aspect for only certain types of T? I am adding aspects in ithe AssemblyInfo.cs, like so:
[assembly: LogMethod(AttributePriority = 0,
AttributeTargetTypeAttributes = MulticastAttributes.Public,
AttributeTargetMemberAttributes = MulticastAttributes.Public,
AttributeTargetElements = MulticastTargets.Method)]

It is not possible to apply generic arguments filtering in a declarative way. For advanced filtering of the aspect targets you can override CompileTimeValidate method on your aspect and filter programmatically.
However, even this is not going to be enough in the case you've described. Let's say you've applied the aspect to a method in MyClass<T>. At this point in compilation the T is not known yet, so it's not possible to perform the check. The concrete T is known when a field or variable of MyClass<T> is declared somewhere else in the code.
The best option I can see in your case is to make the aspect instance-scoped and validate at run-time for each instance of the target class. You can find the sample implementation of this approach below.
[PSerializable]
public class LogMethodAttribute : OnMethodBoundaryAspect, IInstanceScopedAspect
{
private bool disabled;
public override void OnEntry( MethodExecutionArgs args )
{
if ( !this.disabled )
{
Console.WriteLine( "OnEntry: {0}({1})", args.Method.Name, args.Arguments.GetArgument( 0 ) );
}
}
public object CreateInstance( AdviceArgs adviceArgs )
{
LogMethodAttribute clone = (LogMethodAttribute) this.MemberwiseClone();
Type type = adviceArgs.Instance.GetType();
if ( type.IsGenericType )
{
Type[] genericArguments = type.GetGenericArguments();
// Filter out targets where T is string.
if ( genericArguments[0] == typeof( string ) )
{
clone.disabled = true;
}
}
return clone;
}
public void RuntimeInitializeInstance()
{
}
}
class Program
{
static void Main( string[] args )
{
var obj1 = new Class1<int>();
obj1.Method1(1);
var obj2 = new Class1<string>();
obj2.Method1("a");
}
}
[LogMethod(AttributeTargetElements = MulticastTargets.Method)]
public class Class1<T>
{
public void Method1(T a)
{
}
}

Related

C# How To Initialize a generic Class with a type variable

disclaimer I'm a newbie in understanding Reflection.
abstract class BaseClass<T>
{
public abstract T Value { get; }
public virtual bool CheckValue(string input)
{
return true;
}
}
class NotBaseClassA : BaseClass<string>
{
public override string Value { get => "Yes";}
public override bool CheckValue(string input)
{
return 1 == 2;
}
}
class NotBaseClassB : BaseClass<int>
{
public override int Value { get => 1; }
}
class ManyBaseClasses
{
public NotBaseClassB notBaseClassB;
public NotBaseClassA notBaseClassA;
}
class Programm
{
public void Main()
{
foreach (PropertyInfo pi in typeof(ManyBaseClasses).GetProperties())
{
string input = Console.ReadLine();
//Get the generic type of the propertyInfo
//BaseClass<type> bt = new BaseClass<type>();
//Instantiate BaseClass not as a var So I can do
//BaseClass.CheckValue(input)
}
}
}
I'm just trying to do like the title says. So i saw this answer but the answer returns a 'var' but I cant call my CheckValue() function from a type var. (Or i dont think I can?). What i Need is to Instantiate my BaseClass<> with the correct type from a type variable and not as a var, as a proper BaseClass obj so i can then call my functions.
Edit 1 : i've already managed to get the generic type in the variable by doing something like that
public static System.Type GetBaseClassType(this System.Type type)
{
System.Type[] types = new System.Type[]{ };
while (type != null && type != typeof(object) || types.Length == 0)
{
types = type.GetGenericArguments();
if (types.Length > 0)
{
return types[0];
}
type = type.BaseType;
}
return null;
}
The base class is not relevant in this case, as it's abstract, so you actually want to instantiate the derived class.
All you need to do to create it is
Activator.CreateInstance(pi.PropertyType)
Then you will need to use reflection on that result to call CheckValue, because there is no common base type or interface.
It might be easier to extract the non-generic code into a BaseBaseClass which is not generic, which means you don't need reflection for the second step.
abstract class BaseBaseClass
{
public virtual bool CheckValue(string input)
{
return true;
}
}
abstract class BaseClass<T> : BaseBaseClass
{
public abstract T Value { get; }
}
Then you can just do
((BaseBaseClass) Activator.CreateInstance(pi.PropertyType)).CheckValue(someInput)
I've done that before, but it was a long time ago. You have to create instance via reflection and call the method via reflection.
foreach (PropertyInfo pi in typeof(ManyBaseClasses).GetProperties())
{
string input = Console.ReadLine();
//Get the generic type of the propertyInfo
var propType = pi.Type;
Type[] typeArgs = { propType };
var genType = d1.MakeGenericType(typeArgs);
//BaseClass<type> bt = new BaseClass<type>();
object bt = Activator.CreateInstance(genType);
//Instantiate BaseClass not as a var So I can do
//BaseClass.CheckValue(input)
MethodInfo method = typeof(bt).GetMethod("CheckValue"));
method.Invoke(bt, new[] { input });
}

Is there a way to invoke a templated class on an anonymous type?

Here's some code to give the idea.
public class C<T> { }
public class X {
void M() {
var V = new { W = 1 };
var X = new C<V>(); // illegal
}
}
So V is a variable of an anonymous type and I would like to instantiate class C with V as its argument. This requires a type argument, which I cannot supply.
This questions is similar but the answer is not much help in my case: Why can't I instantiate a generic class inferring types from anonymous objects?
The question behind the question is that I'm trying to do what IEnumerable can do.
Should have made it clear: I would really prefer not to do this by manipulating object or Type because you lose the benefits of strong typing and Intellisense.
For anyone interested, the project that needs this is described here: http://www.andl.org/2016/07/andl-net-making-progress/.
You can use type inference if you have a factory method:
public class C<T>
{
public C(T t)
{
// ...
}
}
public static class Factory
{
public static C<T> Create<T>(T t)
{
return new C<T>(t);
}
}
public class Thing
{
void Foo()
{
var x = new { y = "z" };
//var thing = new C(x); - doesn't work, you need to specify the generic parameter
var thing = Factory.Create(x); // T is inferred here
}
}
You can't do that since V is instance of anonymous type, not a type name itself.
You can create this type dynamically (assuming parameterless constructor in C<>):
var X = typeof (C<>)
.MakeGenericType(V.GetType())
.GetConstructor(Type.EmptyTypes)
.Invoke(new object[0]);
You need a type, you can use pass object as a type.
Sample code:
public class C<T>
{
public T _t { get; set; }
public C(T t)
{
_t = t;
}
public void TestMethod()
{
Console.WriteLine(_t.ToString());
}
}
public class X
{
public void M()
{
var V = new { W = 1 };
var X = new C<object>(V); // everything is an object.
X.TestMethod();
}
}

Measuring relational inheritance distance between types

Does anyone know how I could (or if there is an existing algorithm) measure the relational distance between two .NET types?
By this I mean the number of 'steps' in the hierarchical tree that is required to get from object A to object B.
For example, if object A is a Button, and object B is a LinkButton, there would be 2 steps, Button -> WebControl -> LinkButton. Would I need to create my own static inheritance tree and use a path finding algorithm, or is there a way I can dynamically look at the inheritance structure of .NET to calculate the distance between two objects?
Non-generic way (also you don't have to specify parent/child explicitly):
private static int CalulateDistanceOneWay(Type firstType, Type secondType)
{
var chain = new List<Type>();
while (firstType != typeof(object))
{
chain.Add(firstType);
firstType = firstType.BaseType;
}
return chain.IndexOf(secondType);
}
// returns -1 for invalid input, distance between types otherwise
public static int CalculateDistance(Type firstType, Type secondType)
{
int result = CalulateDistanceOneWay(firstType, secondType);
if (result >= 0)
{
return result;
}
return CalulateDistanceOneWay(secondType, firstType);
}
EDIT: update to calculate cousins:
public class DistanceResult
{
public Type SharedAncestor { get; private set; }
public int FirstTypeDistance { get; private set; }
public int SecondTypeDistance { get; private set; }
public DistanceResult(Type sharedAncestor, int firstTypeDistance, int secondTypeDistance)
{
SharedAncestor = sharedAncestor;
FirstTypeDistance = firstTypeDistance;
SecondTypeDistance = secondTypeDistance;
}
}
static DistanceResult CalculateDistance(Type firstType, Type secondType)
{
var firstChain = new List<Type>();
while (firstType != typeof(object))
{
firstChain.Add(firstType);
firstType = firstType.BaseType;
}
firstChain.Add(typeof(object));
var secondChain = new List<Type>();
while(secondType != typeof(object))
{
secondChain.Add(secondType);
secondType = secondType.BaseType;
}
secondChain.Add(typeof(object));
for(var i = 0; i < secondChain.Count; i++)
{
var type = secondChain[i];
int index = firstChain.IndexOf(type);
if (index >= 0)
{
return new DistanceResult(firstChain[index], index, i);
}
}
return null;
}
You can walk through an inheritance path using Type.BaseType. For example:
public static int GetTypeDistance<T, B>(T t, B baseType)
{
if (t is B) // checking if t inherits baseType
{
int distance = 0;
Type curType = t.GetType();
while (curType != typeof(B) && curType != null)
{
distance++;
curType = curType.BaseType;
}
return distance;
}
else { throw new Exception("..."); }
}
Based on the answers from Ondrej and Bényi here are two extension methods to compute the distance from a particular type to either its (indirect) base class type or one of the interfaces implemented by it or one of its base classes.
Usage examples:
Assert.AreEqual( 4, typeof( MultiDictionary<int, int> ).DistanceTo<IEnumerable>() );
Assert.AreEqual( 4, typeof( MultiDictionary<int, int> ).DistanceTo( typeof( IEnumerable ) );
Assert.AreEqual( 2, typeof( StringReader ).DistanceTo( typeof( IDisposable ) ) );
Extension methods:
public static class ExtensionsForType
{
public static int DistanceTo( [CanBeNull] this Type current, [NotNull] Type target )
{
Contract.Requires<ArgumentNullException>( target != null );
// `root` will point to the topmost type which is implementing
// our `target` interface
Type root = current;
// search for topmost base type implementing `target` interface type
// or for the `target` base class type itself
int distance = 0;
while ( current != null && ( target.IsInterface
? current.GetInterfaces().Contains( target )
: current != target ) )
{
root = current;
current = current.BaseType;
distance++;
}
// probably the `current` type does not even derive from / implement
// the target type at all
if ( current == null ) return -1;
// if it's not an interface then we've found it in one of the base classes
if ( !target.IsInterface ) return distance;
// go one step back, because 'current' does not implement
// our target interface anymore
distance--;
// iterate interface "derivations" while the target interface is still
// in the list of implemented interfaces
Type[] interfaces = root.GetInterfaces();
while ( interfaces.Contains( target ) )
{
interfaces = interfaces.SelectMany( i => i.GetInterfaces() ).ToArray();
distance++;
}
return distance;
}
public static int DistanceTo<T>( [CanBeNull] this Type current )
{
return current.DistanceTo( typeof( T ) );
}
}

How to conditionally cast to multiple types in c#

I'm looking at a function with this pattern:
if( obj is SpecificClass1 )
{
((SpecificClass1)obj).SomeMethod1();
}
else if( obj is SpecificClass2 )
{
((SpecificClass2)obj).SomeMethod2();
}
else if( obj is SpecificClass3 )
{
((SpecificClass3)obj).SomeMethod3();
}
and get a code analysis warning: CA1800 Do not cast unnecessarily.
What's a good code pattern I can use to replace this code with that will be performant and concise.
Update
I didn't say, but obj is declared with type object.
I originally asked two questions here. I've split one off (which nobody had yet answered anyway): Why wouldn't the compiler optimize these two casts into one?
Interface
The best way would be to introduce an interface that all the types implement. This is only possible if the signatures match (or you don't have too many differences).
Using as
If creating an interface is not an option, you can get rid of the CA message by using the following pattern (though this also introduces unnecessary casts and therefore degrades performance a bit):
var specClass1 = obj as SpecificClass1;
var specClass2 = obj as SpecificClass2;
var specClass3 = obj as SpecificClass3;
if(specClass1 != null)
specClass1.SomeMethod1();
else if(specClass2 != null)
specClass2.SomeMethod2();
else if(specClass3 != null)
specClass3.SomeMethod3();
You can also change it to this structure (from my point of view, the above is better in terms of readability):
var specClass1 = obj as SpecificClass1;
if (specClass1 != null)
specClass1.SomeMethod1();
else
{
var specClass2 = obj as SpecificClass2;
if (specClass2 != null)
specClass2.SomeMethod2();
else
{
var specClass3 = obj as SpecificClass3;
if (specClass3 != null)
specClass3.SomeMethod3();
}
}
Registering the types in a dictionary
Also, if you have many types that you want to check for, you can register them in a dictionary and check against the entries of the dictionary:
var methodRegistrations = new Dictionary<Type, Action<object> act>();
methodRegistrations.Add(typeof(SpecificClass1), x => ((SpecificClass1)x).SomeMethod1());
methodRegistrations.Add(typeof(SpecificClass2), x => ((SpecificClass2)x).SomeMethod2());
methodRegistrations.Add(typeof(SpecificClass3), x => ((SpecificClass3)x).SomeMethod3());
var registrationKey = (from x in methodRegistrations.Keys
where x.IsAssignableFrom(obj.GetType()).FirstOrDefault();
if (registrationKey != null)
{
var act = methodRegistrations[registrationKey];
act(obj);
}
Please note that the registrations are easily extendable and that you can also call methods with different arguments in the action.
To avoid the double casting you could do the following
var objClass1= obj as SpecificClass1;
if(objClass1!=null)
objClass1.SomeMethod1();
Regarding the pattern you could make all these classes implement a common interface and make your method receive the interface.
public void SomeMethod(ISpecificInterface specific)
{
specific.SomeMethod1();
}
Can you do something like this here?
interface IBaseInterface
{
void SomeMethod();
}
public class Implementer1:IBaseInterface
{
public void SomeMethod()
{
throw new NotImplementedException();
}
}
public class Implementer2 : IBaseInterface
{
public void SomeMethod()
{
throw new NotImplementedException();
}
}
public class Implementer3 : IBaseInterface
{
public void SomeMethod()
{
throw new NotImplementedException();
}
}
And then, in caller code:
IBaseInterface concrete = GetInstance();
concrete.SomeMethod();
and GetInstance would create class objects based on conditions.
Your classes can be inherite a ISomeMethodInterface like this:
public interface ISomeMethodInterface
{
void SomeMethod();
}
public class SpecificClass1 : ISomeMethodInterface
{
//some code
public void SomeMethod()
{
}
}
public class SpecificClass2 : ISomeMethodInterface
{
//some code
public void SomeMethod()
{
}
}
public class SpecificClass3 : ISomeMethodInterface
{
//some code
public void SomeMethod()
{
}
}
And in your call:
((ISomeMethodsInterface)obj).SomeMethod();
The most extensible solution would probably be inheriting the concrete class while implementing an interface with a SomeMethod implementation that calls the correct SomeMethodx method on the inherited class. That way, you'll keep the existing interface while still keeping the existing methods.
public interface ISomething {
void SomeMethod();
}
public SpecificClass1Wrapper : SpecificClass1, ISomething {
void SomeMethod() { SomeMethod1(); }
}
If the objects are wrapped in this way before they're stored in the object reference, a cast to ISomething and a call to SomeMethod() will replace your entire if/else combination.
If the object on the other hand comes from code you have no way of extending and terse but still clear is what you're going for, you could create a simple helper method;
private bool CallIfType<T>(object obj, Action<T> action) where T : class
{
var concrete = obj as T;
if (concrete == null)
return false;
action(concrete);
return true;
}
You can then write the calls as a simple expression;
var tmp = CallIfType<SpecificClass1>(obj, x => x.SomeMethod1()) ||
CallIfType<SpecificClass2>(obj, x => x.SomeMethod2()) ||
CallIfType<SpecificClass3>(obj, x => x.SomeMethod3());
if(tmp)
Console.WriteLine("One of the methods was called");
Ok, bit rough, but:
public class BaseClass{}
public class SubClass1 : BaseClass
{
public void SomeMethod1()
{
}
}
public class SubClass2 : BaseClass
{
public void SomeMethod2()
{
}
}
public class Class1
{
public Class1()
{
var test = new SubClass1();
var lookup = new Dictionary<Type, Action<object>>
{
{typeof (SubClass1), o => ((SubClass1) o).SomeMethod1() },
{typeof (SubClass2), o => ((SubClass2) o).SomeMethod2() }
};
//probably best to check the type exists in the dictionary first,
//maybe wrap up the execution into a class of it's own so it's abstracted away
lookup[test.GetType()](test);
}
}
How about writing a method
public static class ObjectExtensions
{
public static bool TryCast<T>(this object from, out T to) where T : class
{
to = from as T;
return to != null;
}
}
and using it:
SpecificClass1 sc1;
SpecificClass2 sc2;
SpecificClass3 sc3;
if( obj.TryCast(out sc1) )
{
sc1.SomeMethod1();
}
else if( obj.TryCast(out sc2) )
{
sc2.SomeMethod2();
}
else if( obj.TryCast(out sc3) )
{
sc3.SomeMethod3();
}
Not sure if I'm missing a goal, but here's an option that should work.
if( obj is SpecificClass1 sc1 )
{
sc1.SomeMethod1();
}
else if( obj is SpecificClass2 sc2 )
{
sc2.SomeMethod2();
}
else if( obj is SpecificClass3 sc3 )
{
sc3.SomeMethod3();
}
else
{
throw new exception();
}
You can also
switch (obj)
{
case SpecificClass1 sc1:
sc1.SomeMethod1();
break;
case SpecificClass2 sc1:
sc2.SomeMethod2();
break;
case SpecificClass3 sc1:
sc3.SomeMethod3();
break;
default:
throw new Exception();
}

Custom business object comparer

I need to implement mechanism that compares two business objects and return the list of differences (past value, new value, isDifferenceBetter).
Because not all fields of class has to be compared and one fields need to be compared with different function then the other (sometimes < is better sometimes > is better ... ) I figured out that I need to implelemnt custom attribute and give it to each field that has to be compared in this object.
This attribute must have:
- name
- delegate or sth to point to the function which would be applied for comparision (dont know how to do it so far)
So could anyone suggest me if its a good idea? Maybe any other ideas.
Using attributes I would be able to use refflection to iterate through each field with attribute and invoke needed delegate.
thanks for help
bye
See my example below.
May be, it can help you:
namespace ConsoleApplication5
{
class FunctionToCompareAttribute : Attribute
{
public FunctionToCompareAttribute( String className, String methodName )
{
ClassName = className;
MethodName = methodName;
}
public String ClassName
{
get;
private set;
}
public String MethodName
{
get;
private set;
}
}
class ComparableAttribute : Attribute
{
}
class CompareResult
{
}
[Comparable]
class ClassToCompare
{
[FunctionToCompare( "ConsoleApplication5.ClassToCompare", "MyCompareFunction" )]
public String SomeProperty
{
get;
private set;
}
public static CompareResult MyCompareFunction( Object left, Object right, String propertyName )
{
return null;//Comparsion
}
}
class Program
{
static void Main( string[] args )
{
var left = new ClassToCompare();
var right = new ClassToCompare();
var type = typeof( ClassToCompare );
var typeAttributes = type.GetCustomAttributes( typeof( ComparableAttribute ), true );
if ( typeAttributes.Length == 0 )
return;
foreach ( var property in type.GetProperties() )
{
var attributes = property.GetCustomAttributes( typeof( FunctionToCompareAttribute ), true );
if ( attributes.Length == 0 )
continue;
var compareAttribute = attributes[ 0 ] as FunctionToCompareAttribute;
var className = compareAttribute.ClassName;
var methodName = compareAttribute.MethodName;
var compareType = Type.GetType( className );
var method = compareType.GetMethod( methodName, new Type[] { type, type, typeof( String ) } );
var **result** = method.Invoke( null, new Object[] { left, right, property.Name } ) as CompareResult;
}
}
}
}
Do some search about self tracking objects and the way ORMs(like NHibernate) checking an object for dirty fields
Certainly possible but perhaps you should be thinking along more abstract terms. Maybe a pair of attributes [LowerValueIsBetter] and [HigherValueIsBetter] would enable you to express this information in a more cohesive way.

Categories

Resources