Пример #1
0
        public ODataResource CreateEntry(Object entity)
        {
            var odataProperties = new ODataProperty[Accessors.Length];

            for (int i = 0; i < Accessors.Length; i++)
            {
                OePropertyAccessor accessor = Accessors[i];
                Object             value    = accessor.Accessor(entity);

                ODataValue odataValue = null;
                if (value == null)
                {
                    odataValue = new ODataNullValue();
                }
                else if (value.GetType().IsEnum)
                {
                    odataValue = new ODataEnumValue(value.ToString());
                }
                else if (value is DateTime dateTime)
                {
                    switch (dateTime.Kind)
                    {
                    case DateTimeKind.Unspecified:
                        value = new DateTimeOffset(DateTime.SpecifyKind(dateTime, DateTimeKind.Utc));
                        break;

                    case DateTimeKind.Utc:
                        value = new DateTimeOffset(dateTime);
                        break;

                    case DateTimeKind.Local:
                        value = new DateTimeOffset(dateTime.ToUniversalTime());
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("unknown DateTimeKind " + dateTime.Kind.ToString());
                    }
                    odataValue = new ODataPrimitiveValue(value);
                }
                else
                {
                    odataValue = new ODataPrimitiveValue(value);
                }

                odataValue.TypeAnnotation = accessor.TypeAnnotation;
                odataProperties[i]        = new ODataProperty()
                {
                    Name = accessor.Name, Value = odataValue
                };
            }

            return(new ODataResource
            {
                TypeName = _typeName,
                Properties = odataProperties
            });
        }
Пример #2
0
        public ODataResource CreateEntry(Object entity)
        {
            for (int i = 0; i < _accessors.Length; i++)
            {
                OePropertyAccessor accessor = _accessors[i];
                Object             value    = accessor.Accessor(entity);
                if (value is DateTime)
                {
                    value = (DateTimeOffset)(DateTime)value;
                }
                ODataValue odataValue = null;
                if (value == null)
                {
                    odataValue = new ODataNullValue()
                    {
                        TypeAnnotation = accessor.TypeAnnotation
                    }
                }
                ;
                else
                {
                    if (value.GetType().GetTypeInfo().IsEnum)
                    {
                        odataValue = new ODataEnumValue(value.ToString());
                    }
                    else
                    {
                        odataValue = new ODataPrimitiveValue(value);
                    }
                    odataValue.TypeAnnotation = accessor.TypeAnnotation;
                }
                _odataProperties[i].Value = odataValue;
            }

            return(new ODataResource
            {
                TypeName = _typeName,
                Properties = _odataProperties
            });
        }