Esempio n. 1
0
        public static void ReflectProperties(
            Type targetType,
            PropertyDescriptions propertyDescriptions,
            ItemDescriptions itemDescriptions,
            WoopsaConverters customValueTypeConverters = null)
        {
            PropertyInfo[] properties;
            if (!targetType.IsInterface)
            {
                properties = targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            }
            else
            {
                properties = (new Type[] { targetType }).Concat(targetType.GetInterfaces()).SelectMany(i => i.GetProperties()).ToArray();
            }
            foreach (var propertyInfo in properties)
            {
                WoopsaValueTypeAttribute attribute = GetCustomAttribute <WoopsaValueTypeAttribute>(propertyInfo);

                bool isValidWoopsaProperty = false;
                isValidWoopsaProperty = InferWoopsaType(customValueTypeConverters, propertyInfo.PropertyType,
                                                        out WoopsaValueType woopsaPropertyType,
                                                        out WoopsaConverter converter);
                if (attribute != null)
                {
                    woopsaPropertyType    = attribute.ValueType;
                    isValidWoopsaProperty = true;
                }
                if (isValidWoopsaProperty)
                {
                    //This property is a C# property of a valid basic Woopsa Type, it can be published as a Woopsa property
                    PropertyDescription newPropertyDescription = new PropertyDescription(
                        woopsaPropertyType, propertyInfo,
                        !propertyInfo.CanWrite || propertyInfo.GetSetMethod(false) == null,
                        converter);
                    propertyDescriptions.Add(newPropertyDescription);
                }
                else if (!propertyInfo.PropertyType.IsValueType)
                {
                    // This property is not of a WoopsaType, if it is a reference type, assume it is an inner item
                    ItemDescription newItem = new ItemDescription(propertyInfo);
                    itemDescriptions.Add(newItem);
                }
            }
        }
Esempio n. 2
0
        public static void ReflectMethods(
            Type targetType,
            MethodDescriptions methodDescriptions,
            WoopsaConverters customValueTypeConverters = null)
        {
            const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;

            List <MethodInfo> methods = new List <MethodInfo>(targetType.GetMethods(flags));

            foreach (Type inter in targetType.GetInterfaces())
            {
                foreach (MethodInfo method in inter.GetMethods(flags))
                {
                    if (!methods.Contains(method))
                    {
                        methods.Add(method);
                    }
                }
            }
            foreach (var methodInfo in methods)
            {
                WoopsaConverter          converter;
                WoopsaValueTypeAttribute attribute = GetCustomAttribute <WoopsaValueTypeAttribute>(methodInfo);
                WoopsaValueType          woopsaReturnType;
                bool isValidWoopsaMethod = false;
                isValidWoopsaMethod = InferWoopsaType(customValueTypeConverters, methodInfo.ReturnType, out woopsaReturnType, out converter);
                if (attribute != null)
                {
                    woopsaReturnType    = attribute.ValueType;
                    isValidWoopsaMethod = true;
                }
                if (isValidWoopsaMethod)
                {
                    bool argumentsTypeCompatible   = true;
                    ArgumentDescriptions arguments = new ArgumentDescriptions();
                    int parameterIndex             = 0;
                    foreach (var parameter in methodInfo.GetParameters())
                    {
                        WoopsaConverter argumentConverter;
                        WoopsaValueType argumentType;
                        if (InferWoopsaType(customValueTypeConverters, parameter.ParameterType, out argumentType, out argumentConverter))
                        {
                            string parameterName;
                            parameterName = parameter.Name;
                            if (string.IsNullOrEmpty(parameterName))
                            {
                                if (typeof(Array).IsAssignableFrom(targetType))
                                {
                                    if (methodInfo.Name == "Set")
                                    {
                                        if (parameterIndex == 0)
                                        {
                                            parameterName = "index";
                                        }
                                        else if (parameterIndex == 1)
                                        {
                                            parameterName = "value";
                                        }
                                    }
                                    else if (methodInfo.Name == "Get")
                                    {
                                        if (parameterIndex == 0)
                                        {
                                            parameterName = "index";
                                        }
                                    }
                                }
                                if (parameterName == null)
                                {
                                    parameterName = "p" + parameterIndex.ToString();
                                }
                            }
                            ArgumentDescription newArgument = new ArgumentDescription(
                                new WoopsaMethodArgumentInfo(parameterName, argumentType),
                                parameter.ParameterType, argumentConverter);
                            arguments.Add(newArgument);
                        }
                        else
                        {
                            argumentsTypeCompatible = false;
                            break;
                        }
                        parameterIndex++;
                    }
                    if (argumentsTypeCompatible)
                    {
                        MethodDescription newMethod = new MethodDescription(
                            woopsaReturnType, arguments, methodInfo, converter);
                        methodDescriptions.Add(newMethod);
                    }
                }
            }
        }