Ejemplo n.º 1
0
        public static void ApplyFieldDeclaration(FieldDeclaration declaration, object target, bool throwExceptionOnMissingField)
        {
            MethodInfo setterMethod = ReflectUtil.GetSetter(declaration.Name, target.GetType(), declaration.Value.GetType());

            if (setterMethod != null)
            {
                try
                {
                    setterMethod.Invoke(target, new object[] { declaration.Value });
                }
                catch (System.ArgumentException e)
                {
                    throw new ActivitiException("Error while invoking '" + declaration.Name + "' on class " + target.GetType().FullName, e);
                }
                catch (Exception e)
                {
                    throw new ActivitiException("Illegal acces when calling '" + declaration.Name + "' on class " + target.GetType().FullName, e);
                }
                //catch (InvocationTargetException e)
                //{
                //    throw new ActivitiException("Exception while invoking '" + declaration.Name + "' on class " + target.GetType().FullName, e);
                //}
            }
            else
            {
                FieldInfo field = ReflectUtil.GetField(declaration.Name, target);
                if (field == null)
                {
                    if (throwExceptionOnMissingField)
                    {
                        throw new ActivitiIllegalArgumentException("Field definition uses unexisting field '" + declaration.Name + "' on class " + target.GetType().FullName);
                    }
                    else
                    {
                        return;
                    }
                }

                // Check if the delegate field's type is correct
                if (!FieldTypeCompatible(declaration, field))
                {
                    throw new ActivitiIllegalArgumentException("Incompatible type set on field declaration '" + declaration.Name + "' for class " + target.GetType().FullName + ". Declared value has type " + declaration.Value.GetType().FullName + ", while expecting " + field.DeclaringType.Name);
                }
                ReflectUtil.SetField(field, target, declaration.Value);
            }
        }