当前位置: 首页>>代码示例>>C#>>正文


C# Type.GetRuntimeFields方法代码示例

本文整理汇总了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;
        }
开发者ID:anurse,项目名称:AppRT,代码行数:25,代码来源:EnumValueConverter.cs

示例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;
        }
开发者ID:JackieyLi,项目名称:XamlBehaviors,代码行数:30,代码来源:DataBindingHelper.cs

示例3: InjectIntoResxGeneratedApplicationResourcesClass

 public static void InjectIntoResxGeneratedApplicationResourcesClass(Type resxGeneratedApplicationResourcesClass)
 {
     resxGeneratedApplicationResourcesClass.GetRuntimeFields()
         .First(m => m.Name == "resourceMan")
         .SetValue(null, new WindowsRuntimeResourceManager(resxGeneratedApplicationResourcesClass.FullName,
             resxGeneratedApplicationResourcesClass.GetTypeInfo().Assembly));
 }
开发者ID:foxanna,项目名称:SimpleLocationAlarm,代码行数:7,代码来源:WindowsRuntimeResourceManager.cs

示例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);
        }
开发者ID:Tofudebeast,项目名称:xunit,代码行数:27,代码来源:ArgumentFormatter.cs

示例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();
        }
开发者ID:ActivePHOENiX,项目名称:NAudio,代码行数:29,代码来源:FieldDescriptionHelper.cs

示例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();
 }
开发者ID:DevExpress,项目名称:DevExtreme.AspNet.Data,代码行数:9,代码来源:Utils.cs

示例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())
                         );
 }
开发者ID:snipervld,项目名称:StormXamarin,代码行数:9,代码来源:LocalizationService.cs

示例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;
        }
开发者ID:napolini,项目名称:SDK,代码行数:14,代码来源:ConfigurationViewModel.cs

示例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;
        }
开发者ID:DevExpress,项目名称:DevExtreme.AspNet.Data,代码行数:16,代码来源:EFSorting.cs

示例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(_);
                }
            }
开发者ID:aloneguid,项目名称:support,代码行数:23,代码来源:DictionarySerializer.cs

示例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;
 }
开发者ID:Blisse,项目名称:Bandit,代码行数:23,代码来源:DataBindingHelper.cs

示例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
		}
开发者ID:eslo,项目名称:msgpack-cli,代码行数:83,代码来源:SerializationTarget.cs

示例13: GetField

 private static FieldInfo GetField(Type type, string name)
 {
     return type.GetRuntimeFields().FirstOrDefault(m => !m.IsStatic && m.Name == name);
 }
开发者ID:ChrisLakeZA,项目名称:duality,代码行数:4,代码来源:ReflectionInfo.cs

示例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 );
		}
开发者ID:josiahdecker,项目名称:msgpack-cli,代码行数:34,代码来源:SerializationTarget.cs

示例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;
 }
开发者ID:belyansky,项目名称:Caliburn.Light,代码行数:27,代码来源:BindingHelper.cs


注:本文中的System.Type.GetRuntimeFields方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。