GetPropertyConvert<F, P> with private backing fields.

GetPropertyConvert<F, P> with private backing fields.

Old forum URL: forums.lhotka.net/forums/t/11469.aspx


joemorin73 posted on Thursday, July 12, 2012

I have a property using an enum and an int on the private backing field:

        public static readonly PropertyInfo<TransactionStatusEnum> TransactionStatusProperty = RegisterProperty<TransactionStatusEnum>(c => c.TransactionStatusID, "TransactionStatus"TransactionStatusEnum.Payment, RelationshipTypes.PrivateField);
        private int _transactionStatusID = (int)TransactionStatusIDProperty.DefaultValue;
        public TransactionStatusEnum TransactionStatus
        {
            get { return GetPropertyConvert<intTransactionStatusEnum>(TransactionStatusProperty, _transactionStatusID); }
            set { SetPropertyConvert<TransactionStatusEnumint>(TransactionStatusProperty, _transactionStatusID, value); }
        }

My problem arises when I try using the above GetPropertyConvert and SetPropertyConvert.  In the case of the Get property convert, I set the F and P correctly, but the method is expecting the property info to be an int instead of my enum.  Looking at the source:
    protected P GetPropertyConvert<F, P>(PropertyInfo<F> propertyInfo, F field)
    {
      return Utilities.CoerceValue<P>(typeof(F), null, GetProperty<F>(propertyInfo.Name, field, propertyInfo.DefaultValue, Security.NoAccessBehavior.SuppressException));
    }

I am wondering, should the PropertyInfo not be P as the Property is different than the field?

JonnyBee replied on Friday, July 13, 2012

Yes,

 The PropertyInfo should be the type of the private backing field. In Your case int.

joemorin73 replied on Friday, July 13, 2012

I've attempted this without success.  GetPropertyConvert is asking that both the field and the propertyinfo be the same type.  Even with the different field and property typing.

joemorin73 replied on Friday, July 13, 2012

I got this to work by setting the PropertyInfo and RegisterProperty to int as opposed the enum.  This just seems strange to me.  Should the PropertyInfo be the same type as the property or the backing field?

I was going to say property.  But as I'm thinking about it, I suppose the backing field makes sense.  I suppose a little clarity would help here.  Is the PropertyInfo supposed to reflect the Property or the Field?

JonnyBee replied on Saturday, July 14, 2012

Usually these are the same, but the PropertyInfo is supposed to reflect the backing field type.

IE: One backing field could be exposed as 2 different data properties, yet share the same backing field.

F.ex a SmartData field may be exposed as both String and DateTime.
        a ENum field may be exposed as Int or as Enum type.  

Copyright (c) Marimer LLC