MemberInfo GetSetter()
        {
            Debug.Assert(Context.CurrentProperty != null, "Context.CurrentProperty != null");

            const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;

            var propertyType = Context.CurrentProperty.PropertyType;
            var fields       = InstanceType.GetFields(bindingFlags);

            foreach (var field in fields)
            {
                var attrs = field.GetCustomAttributes(typeof(SetValueAttribute), true);

                if (attrs.Length > 0 && field.FieldType.IsSameOrParentOf(propertyType))
                {
                    return(field);
                }
            }

            var props = InstanceType.GetProperties(bindingFlags);

            foreach (var prop in props)
            {
                var attrs = prop.GetCustomAttributes(typeof(SetValueAttribute), true);

                if (attrs.Length > 0 && prop.PropertyType.IsSameOrParentOf(propertyType))
                {
                    return(prop);
                }
            }

            foreach (var field in fields)
            {
                if (field.Name == "Value" && field.FieldType.IsSameOrParentOf(propertyType))
                {
                    return(field);
                }
            }

            foreach (var prop in props)
            {
                if (prop.Name == "Value" && prop.PropertyType.IsSameOrParentOf(propertyType))
                {
                    return(prop);
                }
            }

            var method = InstanceType.GetMethod(false, "SetValue", bindingFlags);

            if (method != null && method.ReturnType == typeof(void))
            {
                return(method);
            }

            throw new TypeBuilderException(string.Format(
                                               "The '{0}' type does not have appropriate setter. See '{1}' member '{2}' of '{3}' type.",
                                               InstanceType.FullName, propertyType.FullName, Context.CurrentProperty.Name, Context.Type.FullName));
        }
Esempio n. 2
0
        private MemberInfo GetSetter()
        {
            const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;

            Type propertyType = Context.CurrentProperty.PropertyType;

            FieldInfo[] fields = InstanceType.GetFields(bindingFlags);

            foreach (FieldInfo field in fields)
            {
                object[] attrs = field.GetCustomAttributes(typeof(SetValueAttribute), true);

                if (attrs.Length > 0 && TypeHelper.IsSameOrParent(field.FieldType, propertyType))
                {
                    return(field);
                }
            }

            PropertyInfo[] props = InstanceType.GetProperties(bindingFlags);

            foreach (PropertyInfo prop in props)
            {
                object[] attrs = prop.GetCustomAttributes(typeof(SetValueAttribute), true);

                if (attrs.Length > 0 && TypeHelper.IsSameOrParent(prop.PropertyType, propertyType))
                {
                    return(prop);
                }
            }

            foreach (FieldInfo field in fields)
            {
                if (field.Name == "Value" && TypeHelper.IsSameOrParent(field.FieldType, propertyType))
                {
                    return(field);
                }
            }

            foreach (PropertyInfo prop in props)
            {
                if (prop.Name == "Value" && TypeHelper.IsSameOrParent(prop.PropertyType, propertyType))
                {
                    return(prop);
                }
            }

            MethodInfo method = TypeHelper.GetMethod(InstanceType, false, "SetValue", bindingFlags);

            if (method != null && method.ReturnType == typeof(void))
            {
                return(method);
            }

            throw new TypeBuilderException(string.Format(
                                               Resources.TypeBuilder_CannotGetSetter, InstanceType.FullName,
                                               propertyType.FullName, Context.CurrentProperty.Name, Context.Type.FullName));
        }
Esempio n. 3
0
        public override QsValue ExclamationOperator(QsValue key)
        {
            if (key.ToString().Equals("TypeName", StringComparison.OrdinalIgnoreCase))
            {
                return(new QsText(InstanceType.Name));
            }

            if (key.ToString().Equals("Properties", StringComparison.OrdinalIgnoreCase))
            {
                QsFlowingTuple f   = new QsFlowingTuple();
                var            mms = InstanceType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

                QsTupleValue[] fefe = new QsTupleValue[mms.Length];

                for (int ix = 0; ix < mms.Length; ix++)
                {
                    fefe[ix].Name = (string)mms[ix].Name;

                    fefe[ix].Value = new QsText(mms[ix].PropertyType.Name);
                }
                if (fefe.Length == 0)
                {
                    return(new QsFlowingTuple());
                }
                return(new QsFlowingTuple(fefe));
            }

            if (key.ToString().Equals("Methods", StringComparison.OrdinalIgnoreCase))
            {
                QsFlowingTuple f   = new QsFlowingTuple();
                var            mms = from m in InstanceType.GetMethods(BindingFlags.Public | BindingFlags.Instance)
                                     where m.IsSpecialName == false
                                     select m;

                QsTupleValue[] fefe = new QsTupleValue[mms.Count()];

                for (int ix = 0; ix < mms.Count(); ix++)
                {
                    fefe[ix].Name = (string)mms.ElementAt(ix).Name;

                    fefe[ix].Value = new QsText(mms.ElementAt(ix).ReturnType.Name);
                }
                if (fefe.Length == 0)
                {
                    return(new QsFlowingTuple());
                }
                return(new QsFlowingTuple(fefe));
            }

            if (key.ToString().Equals("Type", StringComparison.OrdinalIgnoreCase))
            {
                return(new QsObject(InstanceType));
            }
            return(new QsText("Unknown Command"));
        }