Use Decimal Values as Attribute Params in C#

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



Leave a reply



Submit