本文整理汇总了C#中System.Type.GetFields方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetFields方法的具体用法?C# Type.GetFields怎么用?C# Type.GetFields使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.GetFields方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetSerializableMembers2
private static MemberInfo[] GetSerializableMembers2(Type type)
{
// get the list of all fields
FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
int countProper = 0;
for (int i = 0; i < fields.Length; i++)
{
if ((fields[i].Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized)
continue;
countProper++;
}
if (countProper != fields.Length)
{
FieldInfo[] properFields = new FieldInfo[countProper];
countProper = 0;
for (int i = 0; i < fields.Length; i++)
{
if ((fields[i].Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized)
continue;
properFields[countProper] = fields[i];
countProper++;
}
return properFields;
}
else
return fields;
}
示例2: memberGroup
/// <summary>
/// 动态成员分组
/// </summary>
/// <param name="type">目标类型</param>
public memberGroup(Type type)
{
PublicFields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
NonPublicFields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance).getFindArray(value => value.Name[0] != '<');
PublicProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
NonPublicProperties = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance);
}
示例3: GetRuntimeSerializableFields
internal static List<Field> GetRuntimeSerializableFields(Type _objectType, RuntimeSerializableAttribute _runtimeSerializableAttr)
{
List<Field> _serializableFields = null;
lock (typeMemberInfoCache)
{
// If cached value doesnt exist, then use Reflection to get list of RuntimeSerializable fields
if (!typeMemberInfoCache.TryGetValue(_objectType, out _serializableFields))
{
bool _serializeAllPublicFields = false;
bool _serializeAllNonPublicFields = false;
if (_runtimeSerializableAttr != null)
{
_serializeAllPublicFields = _runtimeSerializableAttr.SerializeAllPublicVariables;
_serializeAllNonPublicFields = _runtimeSerializableAttr.SerializeAllNonPublicVariables;
}
// Using reflection fetch all the fields
FieldInfo[] _publicFields = _objectType.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Static);
FieldInfo[] _nonPublicFields = _objectType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Static);
// List holds both public and non-public fields which needs to be serialised
_serializableFields = new List<Field>(_publicFields.Length + _nonPublicFields.Length);
FilterOutNonSerializableFields(_publicFields, _serializeAllPublicFields, ref _serializableFields);
FilterOutNonSerializableFields(_nonPublicFields, _serializeAllNonPublicFields, ref _serializableFields);
// Cache member
typeMemberInfoCache[_objectType] = _serializableFields;
}
}
return _serializableFields;
}
示例4: EmptyAndUnknownMatches
public static void EmptyAndUnknownMatches(Type svo, SingleValueObjectAttribute attr)
{
var emptyValue = svo.GetFields(BindingFlags.Static | BindingFlags.Public).SingleOrDefault(field =>
field.Name == "Empty" &&
field.IsInitOnly &&
field.FieldType == svo);
var unknownValue = svo.GetFields(BindingFlags.Static | BindingFlags.Public).SingleOrDefault(field =>
field.Name == "Unknown" &&
field.IsInitOnly &&
field.FieldType == svo);
var isEmptyMethod = svo.GetMethods(BindingFlags.Instance | BindingFlags.Public).SingleOrDefault(method =>
method.Name == "IsEmpty" &&
method.GetParameters().Length == 0 &&
method.ReturnType == typeof(Boolean));
var isUnknownMethod = svo.GetMethods(BindingFlags.Instance | BindingFlags.Public).SingleOrDefault(method =>
method.Name == "IsUnknown" &&
method.GetParameters().Length == 0 &&
method.ReturnType == typeof(Boolean));
var isEmptyOrUnknownMethod = svo.GetMethods(BindingFlags.Instance | BindingFlags.Public).SingleOrDefault(method =>
method.Name == "IsEmptyOrUnknown" &&
method.GetParameters().Length == 0 &&
method.ReturnType == typeof(Boolean));
if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.HasEmptyValue))
{
Assert.IsNotNull(emptyValue, "{0} should contain a static read-only Empty field.", svo);
Assert.IsNotNull(isEmptyMethod, "{0} should contain a IsEmpty method.", svo);
}
else
{
Assert.IsNull(emptyValue, "{0} should not contain a static read-only Empty field.", svo);
Assert.IsNull(isEmptyMethod, "{0} should not contain a IsEmpty method.", svo);
}
if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.HasUnknownValue))
{
Assert.IsNotNull(unknownValue, "{0} should contain a static read-only Unknown field.", svo);
Assert.IsNotNull(isUnknownMethod, "{0} should contain a IsUnknown method.", svo);
}
else
{
Assert.IsNull(unknownValue, "{0} should not contain a static read-only Unknown field.", svo);
Assert.IsNull(isUnknownMethod, "{0} should not contain a IsUnknown method.", svo);
}
if (attr.StaticOptions.HasFlag(SingleValueStaticOptions.HasEmptyValue) && attr.StaticOptions.HasFlag(SingleValueStaticOptions.HasUnknownValue))
{
Assert.IsNotNull(isEmptyOrUnknownMethod, "{0} should contain a IsEmptyOrUnknown method.", svo);
}
else
{
Assert.IsNull(isEmptyOrUnknownMethod, "{0} should not contain a IsEmptyOrUnknown method.", svo);
}
}
示例5: ListFields
static void ListFields(Type t)
{
Console.WriteLine("***** Fields *****");
FieldInfo[] fi = t.GetFields();
var fieldNames = from f in t.GetFields() select f.Name;
foreach (var name in fieldNames)
Console.WriteLine("->{0}", name);
Console.WriteLine();
}
示例6: GetDependencyProperty
public static DependencyProperty GetDependencyProperty(string name, Type parent, bool caseSensitive = false)
{
const string prop = "Property";
var propertyName = name;
if (!propertyName.ToLower().EndsWith(prop.ToLower()))
{
propertyName += prop;
}
var fieldInfoArray = parent.GetFields();
foreach (var fieldInfo in fieldInfoArray)
{
if (caseSensitive)
{
if (fieldInfo.Name != propertyName) continue;
var dependencyPropertyField = (DependencyProperty)fieldInfo.GetValue(null);
return dependencyPropertyField;
}
else
{
if (!string.Equals(fieldInfo.Name, propertyName, StringComparison.CurrentCultureIgnoreCase)) continue;
var dependencyPropertyField = (DependencyProperty)fieldInfo.GetValue(null);
return dependencyPropertyField;
}
}
throw new Exception("FSR.Reflection.CannotFindDependencyProperty(null, name, caseSensitive)");
}
示例7: ArrayBufferObjectInterleaved
/// <summary>
/// Construct an ArrayBufferObjectInterleaved specifying its item layout on CPU side.
/// </summary>
/// <param name="arrayItemType">
/// A <see cref="Type"/> describing the type of the array item.
/// </param>
/// <param name="hint">
/// An <see cref="BufferObjectHint"/> that specify the data buffer usage hints.
/// </param>
public ArrayBufferObjectInterleaved(Type arrayItemType, BufferObjectHint hint) :
base(arrayItemType, hint)
{
// Get fields for defining array item definition
FieldInfo[] arrayItemTypeFields = arrayItemType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (arrayItemTypeFields.Length == 0)
throw new ArgumentException("no public fields", "arrayItemType");
// Determine array sections stride
int structStride = Marshal.SizeOf(arrayItemType);
for (int i = 0; i < arrayItemTypeFields.Length; i++) {
FieldInfo arrayItemTypeField = arrayItemTypeFields[i];
ArraySection arraySection = new ArraySection(this, arrayItemTypeField.FieldType);
// Determine array section offset
arraySection.ItemOffset = Marshal.OffsetOf(arrayItemType, arrayItemTypeField.Name);
// Determine array section stride
arraySection.ItemStride = new IntPtr(structStride);
// Mission Normalized property management: add attributes?
arraySection.Normalized = false;
ArraySections.Add(arraySection);
}
// Determine array item size
ItemSize = (uint)structStride;
}
示例8: SetType
private void SetType(Type type)
{
try
{
var items = new List<object>();
var values = Enum.GetValues(type);
// First we need to get list of all enum fields
var fields = type.GetFields();
foreach (var field in fields)
{
// Continue only for normal fields
if (field.IsSpecialName)
continue;
// Get the first BrowsableAttribute and add the item accordingly.
var attr = field.GetCustomAttributes(false).OfType<BrowsableAttribute>().FirstOrDefault();
if (attr == null || attr.Browsable)
items.Add(field.GetValue(0));
}
this.ItemsSource = items;
}
catch
{
}
}
示例9: GetValues
private static object[] GetValues( Type enumType )
{
List<object> values = new List<object>();
if( enumType != null )
{
var fields = enumType.GetFields().Where( x => x.IsLiteral );
foreach( FieldInfo field in fields )
{
// Get array of BrowsableAttribute attributes
object[] attrs = field.GetCustomAttributes( typeof( BrowsableAttribute ), false );
if( attrs.Length == 1 )
{
// If attribute exists and its value is false continue to the next field...
BrowsableAttribute brAttr = ( BrowsableAttribute )attrs[ 0 ];
if( brAttr.Browsable == false )
continue;
}
values.Add( field.GetValue( enumType ) );
}
}
return values.ToArray();
}
示例10: GetFieldsAsHeaders
/// <summary>
/// Returns the name or values as a string, of all fields in the given Type
/// </summary>
/// <param name="type">Data type</param>
/// <param name="separator">Separator for each value</param>
/// <param name="parent">Parent name if there are soome sub types of the main Type</param>
/// <param name="obj">If the values are printed in the string, we need the object</param>
/// <returns>Full string with all name/value fields</returns>
public static string GetFieldsAsHeaders(Type type, string separator, string parent = "", object obj = null)
{
FieldInfo[] fieldInfo;
string output = "";
fieldInfo = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (FieldInfo field in fieldInfo)
{
string value = (obj!= null) ? field.GetValue(obj).ToString() : field.Name;
// Loop through sub types as well
if (field.FieldType.GetFields(BindingFlags.Public | BindingFlags.Instance).Length > 0)
{
if (obj != null)
{
output += GetFieldsAsHeaders(field.FieldType, separator, "", obj.GetType().GetField(field.Name).GetValue(obj));
}
else
{
output += GetFieldsAsHeaders(field.FieldType, separator, value + ".", null);
}
}
else
{
output += parent + value + separator;
}
}
return output;
}
示例11: GetNVCFromEnumValue
/// <summary>
/// 根据枚举类型得到其所有的 值 与 枚举定义Description属性 的集合
/// </summary>
/// <param name="enumType"></param>
/// <returns></returns>
public static NameValueCollection GetNVCFromEnumValue(Type enumType)
{
var nvc = new NameValueCollection();
Type typeDescription = typeof(DescriptionAttribute);
FieldInfo[] fields = enumType.GetFields();
foreach (FieldInfo field in fields)
{
if (field.FieldType.IsEnum)
{
string strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString(CultureInfo.InvariantCulture);
object[] arr = field.GetCustomAttributes(typeDescription, true);
string strText;
if (arr.Length > 0)
{
var aa = (DescriptionAttribute)arr[0];
strText = aa.Description;
}
else
{
strText = "";
}
nvc.Add(strValue, strText);
}
}
return nvc;
}
示例12: Generate
public ModelDescription Generate(Type modelType, ModelDescriptionGenerator generator)
{
var enumDescription = new EnumTypeModelDescription
{
Name = ModelNameHelper.GetModelName(modelType),
ModelType = modelType,
Documentation = generator.CreateDefaultDocumentation(modelType)
};
var hasDataContractAttribute = modelType.GetCustomAttribute<DataContractAttribute>() != null;
foreach (var field in modelType.GetFields(BindingFlags.Public | BindingFlags.Static))
{
if (!ModelDescriptionGenerator.ShouldDisplayMember(field, hasDataContractAttribute)) continue;
var enumValue = new EnumValueDescription
{
Name = field.Name,
Value = field.GetRawConstantValue().ToString()
};
if (generator.DocumentationProvider != null)
{
enumValue.Documentation = generator.DocumentationProvider.GetDocumentation(field);
}
enumDescription.Values.Add(enumValue);
}
generator.GeneratedModels.Add(enumDescription.Name, enumDescription);
return enumDescription;
}
示例13: EnumDescriptor
public EnumDescriptor(Type type)
{
_type = type;
_enum2name = new Dictionary<object, string>();
_name2enum = new Dictionary<string, object>();
var names = Enum.GetNames(type);
foreach (string name in names)
{
#if NET40
var member = type.GetFields().Single(x => x.Name == name);
#elif !PCL
var member = type.GetTypeInfo().GetRuntimeFields().Single((x) => x.Name == name);
#else
var member = type.GetTypeInfo().GetDeclaredField(name);
#endif
var value = Enum.Parse(type, name);
var realName = name;
#if NET40
var attr = (EnumValueAttribute)member.GetCustomAttributes(typeof(EnumValueAttribute), false).FirstOrDefault();
#else
var attr = member.GetCustomAttribute<EnumValueAttribute>();
#endif
if (attr != null)
realName = attr.Value;
_enum2name[value] = realName;
_name2enum[realName] = value;
}
}
示例14: FindMembers
public IEnumerable<MemberInfo> FindMembers(
Type type
)
{
foreach (var fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)) {
if (fieldInfo.IsInitOnly || fieldInfo.IsLiteral) { // we can't write
continue;
}
yield return fieldInfo;
}
foreach (var propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)) {
if (!propertyInfo.CanRead || (!propertyInfo.CanWrite && type.Namespace != null)) { // we can't write or it is anonymous...
continue;
}
// skip indexers
if (propertyInfo.GetIndexParameters().Length != 0) {
continue;
}
// skip overridden properties (they are already included by the base class)
var getMethodInfo = propertyInfo.GetGetMethod(true);
if (getMethodInfo.IsVirtual && getMethodInfo.GetBaseDefinition().DeclaringType != type) {
continue;
}
yield return propertyInfo;
}
}
示例15: SqlTextBuilder
public SqlTextBuilder(object obj)
{
this.obj = obj;
this.t = obj.GetType();
this.fInfos = t.GetFields();
this.sqlBuilder = new StringBuilder();
}