Skip to content

Instantly share code, notes, and snippets.

@jfromaniello
Created May 29, 2010 02:27
Show Gist options
  • Save jfromaniello/417971 to your computer and use it in GitHub Desktop.
Save jfromaniello/417971 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Aspects.Dependencies;
using PostSharp.CodeModel;
using PostSharp.Extensibility;
namespace PostSharpIssue
{
[Serializable]
[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
[MulticastAttributeUsage(
MulticastTargets.Class,
TargetMemberAttributes = MulticastAttributes.Public,
AllowMultiple = false,
PersistMetaData = true)]
public class SampleAspectAttribute : InstanceLevelAspect
{
[ImportMember("GetDeclaringTypeName", IsRequired = false, Order = ImportMemberOrder.AfterIntroductions)] public
Func<string> GetDeclaringTypeNameMethod;
[IntroduceMember(OverrideAction = MemberOverrideAction.Ignore, Visibility = Visibility.Family, IsVirtual = true)]
public string GetDeclaringTypeName()
{
return GetDeclaringTypeNameMethod.Method.DeclaringType.Name;
}
[AspectTypeDependency(AspectDependencyAction.Commute, typeof (SampleAspectAttribute))]
[MethodPointcut("GetMethods")]
[OnMethodSuccessAdvice]
public void OnSuccess(MethodExecutionArgs eventArgs)
{
eventArgs.ReturnValue = GetDeclaringTypeName();
}
public IEnumerable<MethodInfo> GetMethods(Type type)
{
var methodInfos = type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Where(m => m.DeclaringType.Equals(type));
return methodInfos;
}
}
[SampleAspect(AttributeInheritance = MulticastInheritance.Strict)]
public class FailingClassBase<T>
{
public virtual string DoSomething()
{
return string.Empty;
}
}
public class FailingClass : FailingClassBase<string>
{
public override string DoSomething()
{
return base.DoSomething();
}
}
[SampleAspect(AttributeInheritance = MulticastInheritance.Strict)]
public class WorkingClassBase
{
public virtual string DoSomething()
{
return string.Empty;
}
}
public class WorkingClass : WorkingClassBase
{
public override string DoSomething()
{
return base.DoSomething();
}
}
[TestFixture]
public class TestCase
{
[Test]
public void this_should_work_but_fails()
{
var test = new FailingClass();
Assert.AreNotEqual(typeof (FailingClass).Name, test.DoSomething());
}
[Test]
public void this_work()
{
var test = new WorkingClass();
Assert.AreNotEqual(typeof(WorkingClass).Name, test.DoSomething());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment