Bug 4.0.1. ObjectFactory --> non-generic LoadProperty

Bug 4.0.1. ObjectFactory --> non-generic LoadProperty

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


rfcdejong posted on Friday, September 10, 2010

After migrating a project to version 4.0.1. i got several exceptions, one of them is because the non-generic LoadProperty is trying to call the generic loadProperty.. but there is a type mismatch.

Type:        System.ArgumentException
Message:     Object of type 'System.Int32' cannot be converted to type 'System.Nullable`1[U4A.Revis.Common.Enumerations.StatusNieuwsbrief]'.
Source:      mscorlib
Stack Trace: at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
   at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
   at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at Csla.ReadOnlyBase`1.LoadProperty(IPropertyInfo propertyInfo, Object newValue) in C:\DEVELOPMENT\Rente_Voorwaarden\Releases\1.4\Lib\Desktop\Csla\ReadOnlyBase.cs:line 1126
   at UNIT4.Framework.Business.U4AReadOnlyBase`1.UNIT4.Framework.Business.Interface.Core.IBusinessObject.LoadProperty(IPropertyInfo propertyInfo, Object newValue) in C:\DEVELOPMENT\Rente_Voorwaarden\Releases\1.4\Framework\Source\Desktop\Framework.Business\U4AReadOnlyBase.cs:line 53
   at UNIT4.Framework.Business.Server.Factories.U4AReadOnlyListBaseFactory`3.LoadProperty(Object obj, IPropertyInfo propertyInfo, Object newValue) in C:\DEVELOPMENT\Rente_Voorwaarden\Releases\1.4\Framework\Source\Desktop\Framework.Business.Server\Factories\U4AReadOnlyListBaseFactory.cs:line 159

var t = this.GetType();
var flags = System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance;
var method = t.GetMethods(flags).Where(c => c.Name == "LoadProperty" && c.IsGenericMethod).FirstOrDefault();
var gm = method.MakeGenericMethod(propertyInfo.Type);
var p = new object[] { propertyInfo, newValue };
gm.Invoke(
this, p);

The old LoadProperty called the FieldManager that called Utilities.CoerceValue if the typed mismatched.

For now i will just override the LoadProperty

rfcdejong replied on Friday, September 10, 2010

i solved it in our abstractionlayer by using the code from FieldManager.LoadFieldData to coerce the value first.

void Interface.Core.IBusinessObject.LoadProperty(IPropertyInfo propertyInfo, object newValue)
{
  if (typeof(IDynamicPropertyInfo).IsAssignableFrom(propertyInfo.GetType())
 
{
    SetDynamicPropertyValue(propertyInfo, newValue);
  }
  else
  {
    Type valueType = (newValue != null) ? valueType = newValue.GetType() : propertyInfo.Type; 
   
var value = Utilities.CoerceValue(propertyInfo.Type, valueType, null, newValue);

   
this.LoadProperty(propertyInfo, value);
  }

Whenever the non-generic LoadProperty implements type coercing i'll revert my changes

RockfordLhotka replied on Friday, September 10, 2010

I am not convinced that I see this as a bug. It is a change, that is for sure.

But the overall change was to make the non-generic LoadProperty work consistently like the generic LoadProperty, which it now does.

rfcdejong replied on Friday, September 10, 2010

yup.. it's a change... working differend as before..

Before it would just coerse the value and now it won't, it's a new restriction. Checking the types with reflection and calling PropertyConvert is another thing.

It's nice that the generic LoadProperty is being called so event hooking etc will work.
Except that i liked that the non-generic would coerse the value whatever kind of property was being loaded with a value.

RockfordLhotka replied on Friday, September 10, 2010

Maybe we need to look at something like a non-generic LoadPropertyConvert?

rfcdejong replied on Friday, September 10, 2010

If u can add that to the wish list then i'm happy. And it's easy to implement .

Copyright (c) Marimer LLC