When i define this:
[Xtensive.Orm.Field(DefaultValue=123456789.12M)]
public decimal MyField {get;set;}
Compiler throws error:
An attribute argument must be a
constant expression, typeof expression
or array creation expression of an
attribute parameter type
But when i use Double type like this:
[Xtensive.Orm.Field(DefaultValue=123456789.12D)]
public double MyField {get;set;}
Everythink compiled and works.
When you look deeper at first example 123456789.12M
is shorthand for decimal value, but cannot be used (by some reason that suffix M for decimal don't work same way like other numeric suffixes D-double,L-long,...) in Attribute as value of property DefaultValue
.
I found "workaround" for this situation here. Solution is to make something what is made in framework type System.ComponentModel.DefaultValueAttribute
, where is constructor System.Type
which define type of value.
My suggestion is to add another optional property to class FieldAttribute, maybe with name DefaultValueType
of type System.Type
. When user do not specify directly this type it will continue to work like now, but when user specify this DefaultValueType
then you make type conversion (by TypeConverter?) to get the real typed value.
In my case i would write this:
[Xtensive.Orm.Field(DefaultValueType=typeof(Decimal), DefaultValue="123456789.12")]
public decimal MyField {get;set;}
and DO converts string "123456789.12" to type Decimal specified in property DefaultValueType
.
Update: Found same thing in attribute Xtensive.Orm.TypeDiscriminatorValue
where property Value is of type object. Maybe there could be also new property ValueType of System.Type
?
asked
Mar 30 '11 at 06:50
Peter Šulek
492●31●32●36
Btw i found this problem when testing my Entity Model Designer :-)
Should we set maximal priority to the issue then? :)
If it is possible to set max priority it would be great, because my tool will then generate non-compilable code. Sure i can set my editor to not allow user to define decimal for a while, but i would like to avoid this.
This won't be complicated. Will implement soon.
Thanks, will wait for it!
may be you can implement default for Entity Fields with Guid(and other) keys?
fails on convertion from string to guid
This change will work either for Guids, you will specify:
if they implement such feature.
2 pil0t, Peter
Sure, will do