You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would also like to implement a test that only certain types can access the system time.
However, the solution mentioned by @simonthum unfortunately does not work for me.
I tried unsuccessfully with Classes().Should().NotCallAny(MethodMembers().That().AreDeclaredIn(typeof(System.DateTimeOffset)));
However, this is reasonable to me since _ = DateTimeOffset.Now is an access to a property.
However, I cannot replace MethodMembers with PropertyMembers because the result is not accepted by NotCallAny.
I wonder if there is a way to check for property access, or is this functionality not (yet) part of ArchUnitNET?
I was wrong -- Properties are a C# feature which are transformed into methods by the compiler. After specifying an empty parameter list and adding the assembly of DateTimeOffset to the architecture, it worked:
I didn't find a way to define the rule strongly typed either. For the meantime I'm using an extension method. Maybe this is an acceptable workaround for others.
// Extension methodpublicstaticIArchRuleNotAccessGetter<TType>(thisObjectsShould<TypesShouldConjunction,IType>builder,object?property,[CallerArgumentExpression("property")]stringtypeAndPropertyName=null!){varnames=typeAndPropertyName.Split('.');if(names[0]!=typeof(TType).Name)thrownewArgumentException($"'{names[1]}' is not part of '{names[0]}'.");returnbuilder.NotCallAny(MethodMembers().That().AreDeclaredIn(typeof(TType)).And().HaveName($"get_{names[1]}()"));}
// Usage example[Fact]publicvoidTest1(){// Remember to add the assembly that contains DateTimeOffset to your `architecture`.vartypes=Types().That().ResideInAssembly(myAssembly);types.Should().NotAccessGetter<DateTimeOffset>(DateTimeOffset.Now).Check(architecture);types.Should().NotAccessGetter<DateTimeOffset>(DateTimeOffset.UtcNow).Check(architecture);}
As far as I can see, since this issue has been opened, getting the property members should now be possible using PropertyMembers().That().AreDeclaredIn(typeof(DateTimeOffset)). But there does not seem to be a fluent method to check that Types().Should().NotAccessAny(PropertyMembers()...), so that may be missing?
Activity
x789 commentedon Feb 28, 2023
I would also like to implement a test that only certain types can access the system time.
However, the solution mentioned by @simonthum unfortunately does not work for me.
I tried unsuccessfully with
Classes().Should().NotCallAny(MethodMembers().That().AreDeclaredIn(typeof(System.DateTimeOffset)));However, this is reasonable to me since
_ = DateTimeOffset.Nowis an access to a property.However, I cannot replace
MethodMemberswithPropertyMembersbecause the result is not accepted byNotCallAny.I wonder if there is a way to check for property access, or is this functionality not (yet) part of ArchUnitNET?
x789 commentedon Mar 2, 2023
I was wrong -- Properties are a C# feature which are transformed into methods by the compiler. After specifying an empty parameter list and adding the assembly of
DateTimeOffsetto the architecture, it worked:I didn't find a way to define the rule strongly typed either. For the meantime I'm using an extension method. Maybe this is an acceptable workaround for others.
simonthum commentedon Mar 20, 2023
@x789 I like that creative solution, although having to actually call the Property may make it difficult to use in some contexts.
alexanderlinne commentedon Nov 28, 2025
Hi, is this still an issue?
As far as I can see, since this issue has been opened, getting the property members should now be possible using
PropertyMembers().That().AreDeclaredIn(typeof(DateTimeOffset)). But there does not seem to be a fluent method to check thatTypes().Should().NotAccessAny(PropertyMembers()...), so that may be missing?