Allowing runtime evaluated expressions in user defined Attributes #4770
Unanswered
adejongh93
asked this question in
Language Ideas
Replies: 3 comments 1 reply
-
See: #4771 Why not using "Tools/Create Guid" in e.g. Visual Studio? |
Beta Was this translation helpful? Give feedback.
1 reply
-
See #343. |
Beta Was this translation helpful? Give feedback.
0 replies
-
If you need "runtime evaluated", you can probably workaround this by providing a dummy class type as argument and internally store runtime generated GUIDs in a class InlineDataAttribute
{
private static Dictionary<Type, Guid> _guids = new();
private readonly Type _type;
public InlineDataAttribute(Type type)
{
_type = type;
}
public Guid Guid
{
get
{
if (!_guids.TryGetValue(_type, out var ret))
{
ret = Guid.NewGuid();
_guids[_type] = ret;
}
return ret;
}
}
}
public class UnitTests
{
private class GuidType {}
[Theory]
[InlineData(typeof(GuidType))]
public void SomeTest(string id)
{
// some Assertions
}
} Note that this does not handle multithread racing. If you don't want to add new type for each method, you may instead combine a type + a |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Background
Working with Attributes InlineData on Xunit we faced the issue of not being able to pass runtime evaluated expressions, for example,
Guid.NewGuid().ToString()
when creating dynamic identifiers.This issue is extended to user defined Attributes.
Proposal
Modify the compiler so the attributes would accept new types and runtime evaluated expressions.
Beta Was this translation helpful? Give feedback.
All reactions