本文整理汇总了C#中System.Type.GetRuntimeFields方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetRuntimeFields方法的具体用法?C# Type.GetRuntimeFields怎么用?C# Type.GetRuntimeFields使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.GetRuntimeFields方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetDisplayName
private string GetDisplayName(object value, Type enumType)
{
FieldInfo[] fields;
if (!_typeCache.TryGetValue(enumType, out fields))
{
fields = enumType.GetRuntimeFields()
.Where(f => f.IsPublic && f.IsStatic)
.ToArray();
_typeCache[enumType] = fields;
}
var matchingField = fields.FirstOrDefault(
f => Equals(f.GetValue(null), value));
if (matchingField == null)
{
throw new MissingMemberException(String.Format("No matching field in the Enum '{0}' for value '{1}'", enumType.FullName, value));
}
string displayName = matchingField.Name;
DisplayNameAttribute attr = matchingField.GetCustomAttribute<DisplayNameAttribute>();
if (attr != null)
{
displayName = attr.Name;
}
return displayName;
}
示例2: GetDependencyProperties
private static IEnumerable<DependencyProperty> GetDependencyProperties(Type type)
{
List<DependencyProperty> propertyList = null;
if (!DataBindingHelper.DependenciesPropertyCache.TryGetValue(type, out propertyList))
{
propertyList = new List<DependencyProperty>();
while (type != null && type != typeof(DependencyObject))
{
foreach (FieldInfo fieldInfo in type.GetRuntimeFields())
{
if (fieldInfo.IsPublic && fieldInfo.FieldType == typeof(DependencyProperty))
{
DependencyProperty property = fieldInfo.GetValue(null) as DependencyProperty;
if (property != null)
{
propertyList.Add(property);
}
}
}
type = type.GetTypeInfo().BaseType;
}
DataBindingHelper.DependenciesPropertyCache[type] = propertyList;
}
return propertyList;
}
示例3: InjectIntoResxGeneratedApplicationResourcesClass
public static void InjectIntoResxGeneratedApplicationResourcesClass(Type resxGeneratedApplicationResourcesClass)
{
resxGeneratedApplicationResourcesClass.GetRuntimeFields()
.First(m => m.Name == "resourceMan")
.SetValue(null, new WindowsRuntimeResourceManager(resxGeneratedApplicationResourcesClass.FullName,
resxGeneratedApplicationResourcesClass.GetTypeInfo().Assembly));
}
示例4: FormatCompledValue
private static string FormatCompledValue(object value, int depth, Type type)
{
if (depth == MAX_DEPTH)
return String.Format("{0} {{ ... }}", type.Name);
var fields = type.GetRuntimeFields()
.Where(f => f.IsPublic && !f.IsStatic)
.Select(f => new { name = f.Name, value = WrapAndGetFormattedValue(() => f.GetValue(value), depth) });
var properties = type.GetRuntimeProperties()
.Where(p => p.GetMethod != null && p.GetMethod.IsPublic && !p.GetMethod.IsStatic)
.Select(p => new { name = p.Name, value = WrapAndGetFormattedValue(() => p.GetValue(value), depth) });
var parameters = fields.Concat(properties)
.OrderBy(p => p.name)
.Take(MAX_OBJECT_PARAMETER_COUNT + 1)
.ToList();
if (parameters.Count == 0)
return String.Format("{0} {{ }}", type.Name);
var formattedParameters = String.Join(", ", parameters.Take(MAX_OBJECT_PARAMETER_COUNT)
.Select(p => String.Format("{0} = {1}", p.name, p.value)));
if (parameters.Count > MAX_OBJECT_PARAMETER_COUNT)
formattedParameters += ", ...";
return String.Format("{0} {{ {1} }}", type.Name, formattedParameters);
}
示例5: Describe
/// <summary>
/// Describes the Guid by looking for a FieldDescription attribute on the specified class
/// </summary>
public static string Describe(Type t, Guid guid)
{
// when we go to .NET 3.5, use LINQ for this
foreach (var f in t
#if NETFX_CORE
.GetRuntimeFields())
#else
.GetFields(BindingFlags.Static | BindingFlags.Public))
#endif
{
if (f.IsPublic && f.IsStatic && f.FieldType == typeof (Guid) && (Guid) f.GetValue(null) == guid)
{
foreach (var a in f.GetCustomAttributes(false))
{
var d = a as FieldDescriptionAttribute;
if (d != null)
{
return d.Description;
}
}
// no attribute, return the name
return f.Name;
}
}
return guid.ToString();
}
示例6: GetPrimaryKey
public static string[] GetPrimaryKey(Type type) {
return new MemberInfo[0]
.Concat(type.GetRuntimeProperties())
.Concat(type.GetRuntimeFields())
.Where(m => m.GetCustomAttributes(true).Any(i => i.GetType().Name == "KeyAttribute"))
.Select(m => m.Name)
.OrderBy(i => i)
.ToArray();
}
示例7: LocalizationService
public LocalizationService(Context context, Type stringType)
{
_strings = stringType.GetRuntimeFields()
.Where(field => field.IsLiteral)
.ToDictionary(
field => field.Name,
field => context.GetString((int)field.GetRawConstantValue())
);
}
示例8: GetItems
private static List<IConfigurationItem> GetItems(DroneConfiguration configuration, object section, Type type)
{
List<IConfigurationItem> configurationItems = new List<IConfigurationItem>();
var configItems = type.GetRuntimeFields()
.Where(field => field.GetValue(section) is IConfigurationItem);
foreach (var configItem in configItems)
{
configurationItems.Add((IConfigurationItem)configItem.GetValue(section));
}
return configurationItems;
}
示例9: FindSortableMember
public static string FindSortableMember(Type entityType) {
var candidates = Enumerable.Concat(
entityType.GetRuntimeProperties()
.Where(i => i.CanRead && i.CanWrite && i.GetGetMethod(true).IsPublic && i.GetSetMethod(true).IsPublic)
.Select(i => new Candidate(i, i.PropertyType)),
entityType.GetRuntimeFields()
.Where(i => i.IsPublic)
.Select(i => new Candidate(i, i.FieldType))
);
var codeFirstId = candidates.FirstOrDefault(IsEFCodeFirstConventionalKey);
if(codeFirstId != null)
return codeFirstId.Member.Name;
return ORDERED_SORTABLE_TYPES.SelectMany(type => candidates.Where(c => c.Type == type)).FirstOrDefault()?.Member.Name;
}
示例10: TypeInfo
public TypeInfo(Type t)
{
//discover properties
IEnumerable<PropertyInfo> properties = t.GetRuntimeProperties();
foreach (PropertyInfo pi in properties)
{
string name = pi.Name;
if(pi.GetMethod != null)
{
_propNameToGetter[name] = _ => pi.GetMethod.Invoke(_, null);
}
}
//discover fields
IEnumerable<FieldInfo> fields = t.GetRuntimeFields();
foreach(FieldInfo fi in fields)
{
string name = fi.Name;
_propNameToGetter[name] = _ => fi.GetValue(_);
}
}
示例11: GetDependencyProperties
private static IEnumerable<DependencyProperty> GetDependencyProperties(Type type)
{
List<DependencyProperty> list = null;
if (!DataBindingHelper.DependenciesPropertyCache.TryGetValue(type, out list))
{
list = new List<DependencyProperty>();
while (type != null && type != typeof(DependencyObject))
{
foreach (FieldInfo current in type.GetRuntimeFields())
if (current.IsPublic && current.FieldType == typeof(DependencyProperty))
{
DependencyProperty dependencyProperty = current.GetValue(null) as DependencyProperty;
if (dependencyProperty != null)
{
list.Add(dependencyProperty);
}
}
}
type = type.GetTypeInfo().BaseType;
}
DataBindingHelper.DependenciesPropertyCache[type] = list;
return list;
}
示例12: GetTargetMembers
// internal for testing
internal static IEnumerable<SerializingMember> GetTargetMembers( Type type )
{
#if DEBUG && !UNITY
Contract.Assert( type != null );
#endif // DEBUG && !UNITY
#if !NETFX_CORE
var members =
type.FindMembers(
MemberTypes.Field | MemberTypes.Property,
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
( member, criteria ) => CheckTargetEligibility( member ),
null
);
var filtered = members.Where( item => Attribute.IsDefined( item, typeof( MessagePackMemberAttribute ) ) ).ToArray();
#else
var members =
type.GetRuntimeFields().Where( f => !f.IsStatic ).OfType<MemberInfo>()
.Concat( type.GetRuntimeProperties().Where( p => p.GetMethod != null && !p.GetMethod.IsStatic ) )
.Where( CheckTargetEligibility );
var filtered = members.Where( item => item.IsDefined( typeof( MessagePackMemberAttribute ) ) ).ToArray();
#endif
if ( filtered.Length > 0 )
{
return
filtered.Select( member =>
new SerializingMember(
member,
new DataMemberContract( member, member.GetCustomAttribute<MessagePackMemberAttribute>() )
)
);
}
if ( type.GetCustomAttributesData().Any( attr =>
attr.GetAttributeType().FullName == "System.Runtime.Serialization.DataContractAttribute" ) )
{
return
members.Select(
item =>
new
{
member = item,
data = item.GetCustomAttributesData()
.FirstOrDefault(
data => data.GetAttributeType().FullName == "System.Runtime.Serialization.DataMemberAttribute" )
}
).Where( item => item.data != null )
.Select(
item =>
{
var name = item.data.GetNamedArguments()
.Where( arg => arg.GetMemberName() == "Name" )
.Select( arg => ( string )arg.GetTypedValue().Value )
.FirstOrDefault();
var id = item.data.GetNamedArguments()
.Where( arg => arg.GetMemberName() == "Order" )
.Select( arg => ( int? ) arg.GetTypedValue().Value )
.FirstOrDefault();
#if SILVERLIGHT
if ( id == -1 )
{
// Shim for Silverlight returns -1 because GetNamedArguments() extension method cannot recognize whether the argument was actually specified or not.
id = null;
}
#endif // SILVERLIGHT
return
new SerializingMember(
item.member,
new DataMemberContract( item.member, name, NilImplication.MemberDefault, id )
);
}
);
}
#if SILVERLIGHT || NETFX_CORE
return members.Where( member => member.GetIsPublic() ).Select( member => new SerializingMember( member, new DataMemberContract( member ) ) );
#else
return
members.Where( item => item.GetIsPublic() && !Attribute.IsDefined( item, typeof( NonSerializedAttribute ) ) )
.Select( member => new SerializingMember( member, new DataMemberContract( member ) ) );
#endif
}
示例13: GetField
private static FieldInfo GetField(Type type, string name)
{
return type.GetRuntimeFields().FirstOrDefault(m => !m.IsStatic && m.Name == name);
}
示例14: GetTargetMembers
private static IEnumerable<SerializingMember> GetTargetMembers( Type type )
{
#if DEBUG && !UNITY
Contract.Assert( type != null, "type != null" );
#endif // DEBUG && !UNITY
#if !NETFX_CORE
var members =
type.FindMembers(
MemberTypes.Field | MemberTypes.Property,
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
null,
null
);
#else
var members =
type.GetRuntimeFields().Where( f => !f.IsStatic ).OfType<MemberInfo>()
.Concat( type.GetRuntimeProperties().Where( p => p.GetMethod != null && !p.GetMethod.IsStatic ) )
.ToArray();
#endif
var filtered = members.Where( item => item.IsDefined( typeof( MessagePackMemberAttribute ) ) ).ToArray();
if ( filtered.Length > 0 )
{
return GetAnnotatedMembersWithDuplicationDetection( type, filtered );
}
if ( type.GetCustomAttributesData().Any( attr =>
attr.GetAttributeType().FullName == "System.Runtime.Serialization.DataContractAttribute" ) )
{
return GetSystemRuntimeSerializationCompatibleMembers( members );
}
return GetPublicUnpreventedMembers( members );
}
示例15: GetDependencyProperties
/// <summary>
/// Gets the dependency properties from <paramref name="type"/>.
/// </summary>
/// <param name="type">The type to inspect.</param>
/// <returns>The dependency properties.</returns>
public static IEnumerable<DependencyProperty> GetDependencyProperties(Type type)
{
List<DependencyProperty> properties;
if (!DependencyPropertyCache.TryGetValue(type, out properties))
{
properties = new List<DependencyProperty>();
for (; type != null && type != typeof (DependencyObject); type = type.GetTypeInfo().BaseType)
{
foreach (var fieldInfo in type.GetRuntimeFields())
{
if (fieldInfo.IsPublic && fieldInfo.FieldType == typeof (DependencyProperty))
{
var dependencyProperty = fieldInfo.GetValue(null) as DependencyProperty;
if (dependencyProperty != null)
properties.Add(dependencyProperty);
}
}
}
DependencyPropertyCache[type] = properties;
}
return properties;
}