Example #1
0
 /// <summary>
 /// Get the member on a parameterized object
 /// </summary>
 /// <typeparam name="T">The expected return value</typeparam>
 /// <param name="obj">The parameterized object</param>
 /// <param name="ckt">The circuit if applicable</param>
 /// <returns></returns>
 public T Get <T>(Parameterized obj, Circuit ckt = null)
 {
     if (ValueType != typeof(T))
     {
         throw new ParameterTypeException(obj, ValueType);
     }
     return((T)Get(obj, ckt));
 }
Example #2
0
        /// <summary>
        /// Set the member on an object
        /// </summary>
        /// <param name="obj">The parameterized object</param>
        /// <param name="value">The parameter value</param>
        /// <param name="ckt">The circuit if applicable</param>
        public void Set(Parameterized obj, object value = null, Circuit ckt = null)
        {
            if (!Access.HasFlag(AccessFlags.Set))
            {
                throw new CircuitException($"Cannot set parameter");
            }
            if (ValueType == typeof(void) && value != null)
            {
                throw new ParameterTypeException(obj, typeof(void));
            }
            else if (value != null && !ValueType.IsAssignableFrom(value.GetType()))
            {
                throw new ParameterTypeException(obj, ValueType);
            }

            switch (MemberType)
            {
            case MemberTypes.Property:
                PropertyInfo pi = Info as PropertyInfo;
                if (IsParameter)
                {
                    ((IParameter)pi.GetValue(obj)).Set(value);
                }
                else
                {
                    pi.SetValue(obj, value);
                }
                break;

            case MemberTypes.Field:
                FieldInfo fi = Info as FieldInfo;
                if (IsParameter)
                {
                    ((IParameter)fi.GetValue(obj)).Set(value);
                }
                else
                {
                    fi.SetValue(obj, value);
                }
                break;

            case MemberTypes.Method:
                MethodInfo mi = Info as MethodInfo;
                switch (Parameters)
                {
                case 0: mi.Invoke(obj, null); break;

                case 1: mi.Invoke(obj, new object[] { value }); break;

                case 2: mi.Invoke(obj, new object[] { ckt, value }); break;
                }
                break;

            default:
                throw new CircuitException($"Invalid type for {Info.Name}");
            }
        }
Example #3
0
        /// <summary>
        /// Get the member on a parameterized object
        /// </summary>
        /// <param name="obj">The parameterized object</param>
        /// <param name="ckt">The circuit if applicable</param>
        /// <returns></returns>
        public object Get(Parameterized obj, Circuit ckt = null)
        {
            if (!Access.HasFlag(AccessFlags.Ask))
            {
                throw new CircuitException($"Cannot ask parameter");
            }

            switch (MemberType)
            {
            case MemberTypes.Property:
                PropertyInfo pi = Info as PropertyInfo;
                if (IsParameter)
                {
                    return(((IParameter)pi.GetValue(obj)).Get());
                }
                else
                {
                    return(pi.GetValue(obj));
                }

            case MemberTypes.Field:
                FieldInfo fi = Info as FieldInfo;
                if (IsParameter)
                {
                    return(((IParameter)fi.GetValue(obj)).Get());
                }
                else
                {
                    return(fi.GetValue(obj));
                }

            case MemberTypes.Method:
                MethodInfo mi = Info as MethodInfo;
                if (Parameters == 0)
                {
                    return(mi.Invoke(obj, null));
                }
                else
                {
                    return(mi.Invoke(obj, new object[] { ckt }));
                }

            default:
                throw new CircuitException($"Invalid type for {Info.Name}");
            }
        }