use decimal values as attribute params in c#?
This is a CLR restriction. Only primitive constants or arrays of primitives can be used as attribute parameters. The reason why is that an attribute must be encoded entirely in metadata. This is different than a method body which is coded in IL. Using MetaData only severely restricts the scope of values that can be used. In the current version of the CLR, metadata values are limited to primitives, null, types and arrays of primitives (may have missed a minor one).
Decimals while a basic type are not a primitive type and hence cannot be represented in metadata which prevents it from being an attribute parameter.
Why decimal is not a valid attribute parameter type?
This is a CLR restriction. Only
primitive constants or arrays of
primitives can be used as attribute
parameters. The reason why is that an
attribute must be encoded entirely in
metadata. This is different than a
method body which is coded in IL.
Using MetaData only severely restricts
the scope of values that can be used.
In the current version of the CLR,
metadata values are limited to
primitives, null, types and arrays of
primitives (may have missed a minor
one).
Taken from this answer by JaredPar.
Decimals while a basic type are not a
primitive type and hence cannot be
represented in metadata which prevents
it from being an attribute parameter.
How does one work around the decimal attribute parameter issue?
Went with using a string to handle the rounding issues as Hans mentioned.
If anyone has a better idea, please answer this question and I will mark it.
Having an actual decimal value as parameter for an attribute (example xUnit.net's [InlineData]
You should be able use the String value in the Attribute and set the Parameter type to Decimal, it get's converted automatically by the Test Framework as far as I can tell.
[Theory]
[InlineData("37.60")]
public void MyDecimalTest(Decimal number)
{
Assert.Equal(number, 37.60M);
}
If this doesn't work then you can manually convert it by passing in a String parameter.
[Theory]
[InlineData("37.60")]
public void MyDecimalTest(String number)
{
var d = Convert.ToDecimal(number);
Assert.Equal(d, 37.60M);
}
Using decimal values in DataRowAttribute
It's because decimal is not a primitive type
The solution is to use strings and then convert your parameters in your test.
Pass decimal as value in WebAPI 2 URL
Got it working by adding a "/"
to the end of the URL!
http://localhost:4627/api/Product/Eligibility/10.5/
Will find this method:
// GET api/Product/Eligibility/10.5/
[Route("api/Product/Eligibility/{amount:decimal}/")]
public decimal GetEligibiilty(decimal amount)
{
return amount;
}
Steve
How do I display a decimal value to 2 decimal places?
decimalVar.ToString("#.##"); // returns ".5" when decimalVar == 0.5m
or
decimalVar.ToString("0.##"); // returns "0.5" when decimalVar == 0.5m
or
decimalVar.ToString("0.00"); // returns "0.50" when decimalVar == 0.5m
How to test with decimal.MaxValue?
Decimal.MaxValue is not a constant, it is a static
readonly
field. Which means you can't use it in attributes as attributes require constants. You'll have to hard code it.
Visual studio will pretend it as a const
but it is actually not.
bool isConstant = typeof (decimal)
.GetField("MaxValue", BindingFlags.Static | BindingFlags.Public)
.IsLiteral;
//isConstant will be false :(
Related Topics
How to Get Img/Src or A/Hrefs Using HTML Agility Pack
Htmlagilitypack and Dynamic Content Issue
C#: Throwing Custom Exception Best Practices
Search Xdocument Using Linq Without Knowing the Namespace
Recursive Linq Query: Select Item and All Children with Subchildren
Ruby Equivalent of C# 'Using' Statement
Open iOS Application from Unity
How to Get All the Possible 3 Letter Permutations
Is There a Tool for Finding Unreferenced Functions (Dead, Obsolete Code) in a C# App
How to Write Out a Text File in C# with a Code Page Other Than Utf-8
Retrieve an Object from Entityframework Without One Field
Insert into SQL Db a String That Contain Special Character '
Perform Button Click Event When User Press Enter Key in Textbox
How to Call R Functions from C# and Retrieve The Result in C#