コード例 #1
0
        public static bool TryGetEnumValue(IXamlType enumType, string value, out object rv)
        {
            rv = null;
            if (long.TryParse(value, out var parsedLong))
            {
                var enumTypeName = enumType.GetEnumUnderlyingType().Name;
                rv = enumTypeName == "Int32" || enumTypeName == "UInt32" ?
                     unchecked ((int)parsedLong) :
                     (object)parsedLong;
                return(true);
            }

            var values = enumType.CustomAttributes.Any(a => a.Type.Name == "FlagsAttribute") ?
                         value.Split(',').Select(x => x.Trim()).ToArray() :
                         new[] { value };
            object cv = null;

            for (var c = 0; c < values.Length; c++)
            {
                var enumValueField = enumType.Fields.FirstOrDefault(f => f.Name == values[c]);
                if (enumValueField == null)
                {
                    return(false);
                }
                var enumValue = GetLiteralFieldConstantValue(enumValueField);
                if (c == 0)
                {
                    cv = enumValue;
                }
                else
                {
                    cv = Or(cv, enumValue);
                }
            }

            rv = cv;
            return(true);
        }