本文整理汇总了C#中System.Reflection.FieldInfo.IsDefined方法的典型用法代码示例。如果您正苦于以下问题:C# FieldInfo.IsDefined方法的具体用法?C# FieldInfo.IsDefined怎么用?C# FieldInfo.IsDefined使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.FieldInfo
的用法示例。
在下文中一共展示了FieldInfo.IsDefined方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Visit
public void Visit(string settingsNamesapce, string fieldPath, FieldInfo rawSettingsField, object rawSettings)
{
// Skip 'sealed' fields.
if (rawSettingsField.IsDefined<SealedAttribute>()) return;
if (rawSettingsField.FieldType == typeof(string))
{
var originalString = (string)rawSettingsField.GetValue(rawSettings);
if (originalString != null)
{
var expandedString = ExpandVariables(originalString, _variables);
rawSettingsField.SetValue(rawSettings, expandedString);
}
}
else if (rawSettingsField.FieldType == typeof(string[]))
{
string[] arr = (string[])rawSettingsField.GetValue(rawSettings);
for (int i = 0; i < arr.Length; i++)
{
string originalString = arr[i];
if (originalString != null)
{
var expandedString = ExpandVariables(originalString, _variables);
arr[i] = expandedString;
}
}
}
}
示例2: CanWriteField
private bool CanWriteField(FieldInfo field)
{
if (field.IsDefined(typeof(CompilerGeneratedAttribute), false))
{
var flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
PropertyInfo info = this.type.GetProperty(field.Name.Substring(1, field.Name.IndexOf('>') - 1), flags);
return !info.IsDefined(typeof(IgnoreSerializeAttribute), false) &&
!info.PropertyType.IsSubclassOf(typeof(Delegate));
}
return !field.IsDefined(typeof(IgnoreSerializeAttribute), false) &&
!field.FieldType.IsSubclassOf(typeof(Delegate));
}
示例3: Visit
public override void Visit(string settingsNamespace, string fieldPath, FieldInfo defaultsField, object defaults, FieldInfo targetField, object target)
{
if (targetField.IsDefined<SealedAttribute>()) return;
object targetValue = targetField.GetValue(target);
object defaultValue = defaultsField.GetValue(defaults);
bool isFinalPath =
targetField.IsDefined<RefAttribute>() ||
defaultValue == null ||
targetValue == null ||
!defaultValue.GetType().IsSettingsType();
if (targetValue == null && defaultValue != null && isFinalPath)
{
targetField.SetValue(target, defaultsField.GetValue(defaults));
_initializedFields.Add(Tuple.Create(target, targetField));
}
}
示例4: Visit
public void Visit(string settingsNamesapce, string fieldPath, FieldInfo refinedSettingsField, object refinedSettings, FieldInfo rawSettingsField, object rawSettings)
{
if (refinedSettingsField.IsDefined<RefAttribute>()) return;
object rawValue = null;
object refinedValue = refinedSettingsField.GetValue(refinedSettings);
if (refinedValue != null)
{
Type refinedValueType = refinedValue.GetType();
if (refinedValueType.IsSettingsType())
{
rawValue = SettingsConstruction.CreateSettingsObject(refinedValue, rawSettingsField, _typeMappings);
}
else if (refinedValueType.IsSettingsOrObjectArrayType())
{
rawValue = SettingsConstruction.CreateSettingsArray((Array)refinedValue, rawSettingsField, _typeMappings);
}
else if (refinedValueType != typeof(string) && rawSettingsField.FieldType == typeof(string))
{
rawValue = ToString(refinedSettingsField, refinedSettings);
}
else
{
rawValue = refinedSettingsField.GetValue(refinedSettings);
}
rawSettingsField.SetValue(rawSettings, rawValue);
}
}
示例5: TryGetName
public static NameType TryGetName(ReflectedType dt, FieldInfo fi, out string name)
{
Debug.Assert(dt.IsSubclassOf(DynamicType.GetDeclaringType(fi)));
NameType nt = NameType.PythonField;
name = null;
// hide MinValue & MaxValue on int, Empty on string, Epsilon, Min/Max, etc.. on double
if (fi.DeclaringType == typeof(string) ||
fi.DeclaringType == typeof(int) ||
fi.DeclaringType == typeof(double) ||
fi.IsDefined(typeof(PythonHiddenFieldAttribute), false)) nt = NameType.Field;
string namePrefix = "";
if (fi.IsPrivate || (fi.IsAssembly && !fi.IsFamilyOrAssembly)) {
if (!Options.PrivateBinding) {
return NameType.None;
} else {
// mangle protectes to private
namePrefix = "_" + dt.Name + "__";
nt = NameType.Field;
}
}
name = namePrefix + fi.Name;
return nt;
}
示例6: Visit
public void Visit(string settingsNamesapce, string fieldPath, FieldInfo refinedSettingsField, object refinedSettings, FieldInfo rawSettingsField, object rawSettings)
{
if (refinedSettingsField.IsDefined<RefAttribute>())
{
if (refinedSettingsField.FieldType.IsArray) ResolveReferenceArray(settingsNamesapce, fieldPath, rawSettingsField, rawSettings, refinedSettingsField, refinedSettings);
else ResolveReference(settingsNamesapce, fieldPath, rawSettingsField, rawSettings, refinedSettingsField, refinedSettings);
}
}
示例7: Visit
public override void Visit(string settingsNamesapce, string fieldPath, FieldInfo refinedSettingsField, object refinedSettings, FieldInfo rawSettingsField, object rawSettings)
{
if (refinedSettingsField.IsDefined<RefAttribute>())
{
if (refinedSettingsField.FieldType.IsSettingsOrObjectType()) PackReference(fieldPath, refinedSettingsField, refinedSettings, rawSettingsField, rawSettings);
else if (refinedSettingsField.FieldType.IsSettingsOrObjectArrayType()) PackReferenceArray(fieldPath, refinedSettingsField, refinedSettings, rawSettingsField, rawSettings);
else throw new ConfigurationException("Did not expect to get here.");
}
}
示例8: GetAttributesTextFor
public virtual IEnumerable<string> GetAttributesTextFor(FieldInfo field, Usage defaultUsage, ParsingPolicyAttribute[] parsingPolicies)
{
var result = new List<string>();
var fieldType = field.FieldType;
string usageAttribute = null;
bool isForcedUsage = defaultUsage == Usage.ForceRequired || defaultUsage == Usage.ForceOptional;
if (isForcedUsage)
{
if (defaultUsage == Usage.ForceRequired) usageAttribute = AttributeBuilder.GetTextFor<RequiredAttribute>();
else if (defaultUsage == Usage.ForceOptional) usageAttribute = AttributeBuilder.GetTextFor<OptionalAttribute>();
}
else if (fieldType.IsValueType && Nullable.GetUnderlyingType(field.FieldType) == null)
{
usageAttribute = AttributeBuilder.GetTextFor<RequiredAttribute>();
}
else if (Nullable.GetUnderlyingType(field.FieldType) != null)
{
usageAttribute = AttributeBuilder.GetTextFor<OptionalAttribute>();
}
else if (!field.IsDefined<RequiredAttribute>() && !field.IsDefined<OptionalAttribute>())
{
if (defaultUsage == Usage.Required) usageAttribute = AttributeBuilder.GetTextFor<RequiredAttribute>();
else if (defaultUsage == Usage.Optional) usageAttribute = AttributeBuilder.GetTextFor<OptionalAttribute>();
}
if (usageAttribute != null)
result.Add(usageAttribute);
var attributes = field.GetCustomAttributes().ToArray();
var attributesData = field.GetCustomAttributesData();
for (int i = 0; i < attributes.Length; i++)
{
// Skip any usage attributes, if field usage is forced by the calling method.
bool isUsageAttribute = attributes[i] is OptionalAttribute || attributes[i] is RequiredAttribute;
if (isForcedUsage && isUsageAttribute) continue;
// Get c# compatible attribute text.
string attributeText = CommonAttributeTranslator.Translate(attributes[i], attributesData[i], field);
if (attributeText != null)
result.Add(attributeText);
}
return result;
}
示例9: Visit
public override void Visit(string settingsNamesapce, string fieldPath, FieldInfo refinedSettingsField, object refinedSettings, FieldInfo rawSettingsField, object rawSettings)
{
if (!refinedSettingsField.IsDefined<RequiredAttribute>()) return;
bool fieldInitialized =
rawSettingsField != null && rawSettingsField.GetValue(rawSettings) != null ||
// Some refined fields could be initialized through applyDefaults or applyOverrides.
_defaultedAndOverridenFields.Contains(Tuple.Create(refinedSettings, refinedSettingsField));
if (!fieldInitialized)
throw new ConfigurationException("Missing required field '{0}'.", fieldPath);
}
示例10: TryGetName
public static NameType TryGetName(PythonType dt, FieldInfo fi, out string name) {
NameType nt = NameType.PythonField;
name = fi.Name;
// hide MinValue & MaxValue on int, Empty on string, Epsilon, Min/Max, etc.. on double
if (fi.DeclaringType == typeof(string) ||
fi.DeclaringType == typeof(int) ||
fi.DeclaringType == typeof(double) ||
fi.IsDefined(typeof(PythonHiddenAttribute), false)) nt = NameType.Field;
string namePrefix = "";
if (fi.IsPrivate || (fi.IsAssembly && !fi.IsFamilyOrAssembly)) {
// mangle protectes to private
namePrefix = "_" + dt.Name + "__";
nt = NameType.Field;
}
name = namePrefix + name;
return nt;
}
示例11: GetElementSetter
static IPoolElementSetter GetElementSetter(object element, FieldInfo field, HashSet<object> toIgnore)
{
if (element == null)
return new PoolElementSetter(element);
else if (toIgnore.Contains(element))
throw new InitializationCycleException(field);
var copier = CopyUtility.GetCopier(element.GetType());
if (copier != null)
return new PoolElementCopierSetter(copier, element);
else if (field.IsDefined(typeof(InitializeContentAttribute), true))
{
if (!(element is ValueType))
toIgnore.Add(element);
return new PoolElementContentSetter(element.GetType(), GetSetters(element, toIgnore));
}
else
return new PoolElementSetter(element);
}
示例12: GetAttributesTextFor
public override IEnumerable<string> GetAttributesTextFor(FieldInfo field, Usage defaultUsage, ParsingPolicyAttribute[] parsingPolicies)
{
var res = new List<string>(base.GetAttributesTextFor(field, defaultUsage, parsingPolicies));
var fieldType = field.FieldType;
var renameRule = field.GetCustomAttribute<RenameAttribute>();
string fieldName = field.GetCustomAttribute<NameAttribute>()?.Name ?? field.Name;
if (!field.IsDefined<RefAttribute>())
{
if (field.IsPolymorphic())
{
Type attributeType = !fieldType.IsArray || field.IsDefined<InlineAttribute>() ? typeof(XmlElementAttribute) : typeof(XmlArrayItemAttribute);
foreach (var t in field.GetKnownSerializableTypes())
res.Add(GetItemAttributeText(attributeType, t, renameRule));
}
else if (
field.FieldType.IsArray &&
!field.IsDefined<ConverterAttribute>() &&
!field.IsDefined<ParserAttribute>() &&
!parsingPolicies.Any(p => p.CanParse(field.FieldType)))
{
Type attributeType = field.IsDefined<InlineAttribute>() ? typeof(XmlElementAttribute) : typeof(XmlArrayItemAttribute);
Type itemTypeName = field.FieldType.GetElementType();
res.Add(GetItemAttributeText(attributeType, itemTypeName, renameRule));
}
}
var rawFieldType = field.GetRawFieldType(parsingPolicies);
if (rawFieldType.IsSimple())
{
res.Add(AttributeBuilder.GetTextFor<XmlAttributeAttribute>(fieldName));
}
else if (!res.Any(a => a.Contains(nameof(XmlElementAttribute))))
{
if (rawFieldType.IsArray)
res.Add(AttributeBuilder.GetTextFor<XmlArrayAttribute>(fieldName));
else
res.Add(AttributeBuilder.GetTextFor<XmlElementAttribute>(fieldName));
}
if (field.IsDefined<HiddenAttribute>())
res.Add(AttributeBuilder.GetTextFor<XmlIgnoreAttribute>());
return res.Where(a => a != null);
}
示例13: Visit
public void Visit(string settingsNamespace, string fieldPath, FieldInfo rawSettingsField, object rawSettings)
{
// Skip 'sealed' fields.
if (rawSettingsField.IsDefined<SealedAttribute>()) return;
if (rawSettingsField.FieldType == typeof(string))
{
string value = (string)rawSettingsField.GetValue(rawSettings);
if (value != null)
{
string expandedValue = Environment.ExpandEnvironmentVariables(value);
rawSettingsField.SetValue(rawSettings, expandedValue);
}
}
else if (rawSettingsField.FieldType == typeof(string[]))
{
var arr = (string[])rawSettingsField.GetValue(rawSettings);
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] != null) arr[i] = Environment.ExpandEnvironmentVariables(arr[i]);
}
}
}
示例14: SizeOf
private static int SizeOf(FieldInfo field)
{
if (field.IsDefined(typeof(TagBlockFieldAttribute), false)) { return 8; }
if (field.IsDefined(typeof(TagReferenceAttribute), false)) { return 8; }
else if (field.IsDefined(typeof(MarshalAsAttribute), false))
{
var marshalAsAttribute = field.GetCustomAttributes(typeof(MarshalAsAttribute), false)[0] as MarshalAsAttribute;
if (marshalAsAttribute.Value == UnmanagedType.ByValArray)
{
var elementSize = SizeOf(field.FieldType.GetElementType());
var count = marshalAsAttribute.SizeConst;
return elementSize * count;
}
}
if (field.FieldType.IsEnum) return SizeOf(Enum.GetUnderlyingType(field.FieldType));
return SizeOf(field.FieldType);
}
示例15: CustomAttributeDefined
private static bool CustomAttributeDefined(FieldInfo field)
{
return field.IsDefined(typeof(CustomMarshalAsAttribute), false);
}