public bool TestFragment <T>(T value) { FieldInfo[] fragments = GetType().GetFields(); foreach (FieldInfo fragment in fragments) { if (fragment.FieldType == typeof(T)) { return(Util.FlagsTest <T>((T)fragment.GetValue(this), value)); } } return(false); }
public bool TestFragment <T>(T value) { FieldInfo[] fragments = GetType().GetFields(); var valueType = value.GetType(); foreach (FieldInfo fragment in fragments) { if (valueType.IsAssignableFrom(fragment.FieldType)) { return(Util.FlagsTest <T>((T)fragment.GetValue(this), value)); } } return(false); }
public static T GetObject <T>(List <Object> options, DefaultTarget defaultTarget) { for (int i = options.Count - 1; i >= 0; --i) { object option = options[i]; if (option is T) { return((T)option); } } // no found, return the default Type optionType = typeof(T); // for class type , default value is NULL; if (optionType.IsClass) { return(default(T)); } // find the default options FieldInfo[] optionTypeFields = optionType.GetFields(); foreach (FieldInfo field in optionTypeFields) { object[] attributes = s_cachedDefaultAttributes.GetOrAdd(field, fi => fi.GetCustomAttributes(typeof(Options.Default), true)); foreach (Default defaultOption in attributes) { if (Util.FlagsTest(defaultOption.DefaultTarget, defaultTarget)) { object fieldValue = field.GetValue(optionType); return((T)fieldValue); } } } throw new Error("Not default value found for options: " + optionType.Name + " Default Options is " + options); }
private static bool SelectOptionImpl(bool throwIfNotFound, Configuration conf, OptionAction[] options) { // Get the type of current options and make sure they are all off the same type Type optionType = null; foreach (OptionAction optionAction in options) { if (optionType == null) { optionType = optionAction.Value.GetType(); } else if (optionType != optionAction.Value.GetType()) { throw new Error("SelectOption may only be called of value of the same type"); } } if (optionType == null) { throw new Error(); } FieldInfo[] optionTypeFields = optionType.GetFields(); // find the latest added option of this type for (int i = conf.Options.Count - 1; i >= 0; i--) { object latestOption = conf.Options[i]; if (latestOption.GetType() == optionType) { foreach (FieldInfo field in optionTypeFields) { if (field.FieldType != optionType) { continue; } object fieldValue = field.GetValue(optionType); if (fieldValue.Equals(latestOption)) { object[] attributes = s_cachedDevEnvAttributes.GetOrAdd(field, fi => fi.GetCustomAttributes(typeof(Options.DevEnvVersion), true)); foreach (Options.DevEnvVersion version in attributes) { if (version.minimum > conf.Compiler) { throw new Error(optionType + " " + latestOption + " is not compatible with your DevEnv (" + conf.Compiler + ")"); } } break; } } foreach (OptionAction optionAction in options) { if (optionAction.Value.Equals(latestOption)) { optionAction.Action(); return(true); } } } } // find the default options foreach (FieldInfo field in optionTypeFields) { object[] attributes = s_cachedDefaultAttributes.GetOrAdd(field, fi => fi.GetCustomAttributes(typeof(Options.Default), true)); foreach (Options.Default defaultOption in attributes) { if (Util.FlagsTest(defaultOption.DefaultTarget, conf.DefaultOption)) { object fieldValue = field.GetValue(optionType); foreach (OptionAction optionAction in options) { if (optionAction.Value.Equals(fieldValue)) { optionAction.Action(); return(true); } } } } } if (throwIfNotFound) { throw new Error("Not default value found for options: " + optionType.Name + " Default Options is " + conf.DefaultOption); } return(false); }