저는 두 가지 포스트 샤프 측면이 서로 작동하는 시나리오를 디자인하고 있습니다. 하나의 측면 (FirstAspect
코드 아래에 있음) 인터페이스를 소개 할 의미가 있고 다른 측면 (코드 아래에 SecondAspect
) 첫 번째 측면에서 도입 된 인터페이스로 작동하도록되어 있습니다.빌드 시간 동안 PostSharp에 의해 소개 된 인터페이스 또는 멤버 인터페이스에 액세스 할 수 있습니까?
그러나 첫 번째 측면에서 도입 된 인터페이스는 두 번째 측면에서 사용 가능한 것으로 보이지 않습니다. FirstAspect
에 의해 도입 된 인터페이스는 한 번에 SecondAspect
에 사용할 수 없습니다로,
public class Tests
{
[Fact]
public void Verify()
{
// Not really all that significant as the current code does not compile correctly:
var sut = new MyClass();
Assert.True(sut is IInterface);
}
public interface IInterface
{
void HelloWorld();
}
[IntroduceInterface(typeof(IInterface))]
public class FirstAspect : InstanceLevelAspect, IInterface, IAspectProvider
{
public void HelloWorld() {}
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
// Implementing IAspectProvider appears to ensure this aspect is processed first.
// This may be a bug.
// Please see: http://support.sharpcrafters.com/discussions/problems/3365-runtimeinitialize-does-not-follow-ordering-rules#comment_40824072
// for more information.
yield break;
}
}
[AspectTypeDependency(AspectDependencyAction.Order, AspectDependencyPosition.After, typeof(FirstAspect))]
public class SecondAspect : InstanceLevelAspect, IAspectProvider
{
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
var type = (Type)targetElement;
if (!typeof(IInterface).GetTypeInfo().IsAssignableFrom(type))
{
// This is currently being thrown, as MyClass does not implement
// IInterface when the AppDomain is first loaded and initialized:
throw new InvalidOperationException($"Does not implement {typeof(IInterface)}");
}
// How to access the weaved elements from FirstAspect? ...
yield break;
}
}
[FirstAspect, SecondAspect]
class MyClass {}
}
내가 구축
의 SecondAspect.ProvideAspects
의 InvalidOperationException
가 발생합니다 : 여기
MyClass
유형으로 바뀌 었음에도 불구하고
유형은 현재 AppDomain 내에있는 형식이므로로드 된 인터페이스는 구현 된 것으로 표시되지 않습니다.
내가 찾고있는 것은 빌드 중에 대상 요소에 알려진 모든 인터페이스와 멤버를 액세스하고 찾을 수있는 기능입니다.
ReflectionSearch
으로 보았는데, 내가 찾고있는 것에 가깝지만이 API를 호출 할 때 weaved 요소를 고려하지 않은 것 같습니다. 예를 들어 ReflectionSearch.GetMembersOfType
전화를 걸면 IInterface.HelloWorld
이 MyClass
(위의 예에서는 FirstAspect
으로 표시됨)에 표시되지 않습니다.
빌드 타임 중에 PostSharp에서 소개/weaved 요소에 액세스하려면 사용해야하는 또 다른 API가 있습니까? 이것은 가능한가?
포스트 샤프 측면에는 원래 유형과 메서드 만 표시되므로 빌드시에 weaved 인터페이스를 보는 방법은 없습니다. SecondAspect에서 소개 된 인터페이스로 무엇을하고 싶습니까? –
답장을 보내 주셔서 감사합니다. @ JakubLinhart. 특정 인터페이스가 내 클래스에 적용되었는지 확인한 다음 해당 인터페이스가 적용되면 평소처럼 여러 가지 측면을 적용합니다. 이 일을하는 또 다른 방법을 알아야 할 것 같은데요? –
'FirstAspect' 커스텀 애트리뷰트의 존재 여부 확인은 어떻게 되나요? IInterface를 확인 하시겠습니까? 또는 도입 된 방법에 어떤면을 적용하고 싶습니까? –