本文整理汇总了C#中System.Type.GetProperties方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetProperties方法的具体用法?C# Type.GetProperties怎么用?C# Type.GetProperties使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.GetProperties方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: ScanExpressions
/// <summary>
/// Recursivly scan the types to build up a list of expressions to access everything.
/// </summary>
/// <param name="streamType"></param>
/// <param name="expressionToAccess"></param>
/// <param name="visitor"></param>
protected static void ScanExpressions (Type streamType, Expression expressionToAccess, Action<Expression> visitor)
{
// If this is a leaf, then dump it.
if (streamType.TypeIsEasilyDumped())
{
visitor(expressionToAccess);
}
// If it is a tuple, then we will have to go down one.
else if (streamType.Name.StartsWith("Tuple"))
{
var targs = streamType.GenericTypeArguments.Zip(Enumerable.Range(1, 100), (t, c) => Tuple.Create(t, c));
foreach (var templateType in targs)
{
var access = Expression.PropertyOrField(expressionToAccess, $"Item{templateType.Item2}");
ScanExpressions(templateType.Item1, access, visitor);
}
}
// Now look at the fields and properties.
else if ((streamType.GetFields().Length + streamType.GetProperties().Length) > 0)
{
foreach (var fName in (streamType.GetFieldsInDeclOrder().Select(f => f.Name).Concat(streamType.GetProperties().Select(p => p.Name))))
{
var access = Expression.PropertyOrField(expressionToAccess, fName);
ScanExpressions(access.Type, access, visitor);
}
}
// Really bad if we get here!
else
{
throw new InvalidOperationException($"Do not know how to generate values for a file from a sequence of {streamType.Name} objects!");
}
}
示例3: GetProperties
private static bool GetProperties(Type type, List<string> methodPath, List<string> properties)
{
if (null == type)
return false;
if (methodPath.Count == 0)
{
properties.Clear();
properties.AddRange(type.GetProperties().Select(pro2 => pro2.Name));
return true;
}
foreach (PropertyInfo prop in type.GetProperties())
{
Trace.WriteLine(prop.PropertyType.Name);
if (prop.Name == methodPath[0])
{
methodPath.RemoveAt(0);
return GetProperties(prop.PropertyType, methodPath, properties);
}
}
properties.Clear();
return false;
}
示例4: FindProperty
private PropertyInfo FindProperty(Type objectType, string propertyName)
{
PropertyInfo property = objectType.GetProperties().FirstOrDefault(x => x.Name.Equals(propertyName));
if (property == null)
{
IEnumerable<PropertyInfo> props = objectType.GetProperties().Where(x => x.GetCustomAttribute<JsonPropertyAttribute>() != null);
if(props != null)
{
foreach(PropertyInfo prop in props)
{
JsonPropertyAttribute jsonAttribute = prop.GetCustomAttribute<JsonPropertyAttribute>();
if(jsonAttribute.PropertyName.Equals(propertyName))
{
property = prop;
break;
}
}
}
if(property == null)
{
throw new PropertyNotFoundException(objectType, propertyName);
}
}
return property;
}
示例5: FindDatePropertyInfosForType
private static EntityDateTimePropertiesInfo FindDatePropertyInfosForType(Type entityType)
{
var datetimeProperties = entityType.GetProperties()
.Where(property =>
(property.PropertyType == typeof(DateTime) ||
property.PropertyType == typeof(DateTime?)) &&
property.CanWrite
).ToList();
var complexTypeProperties = entityType.GetProperties()
.Where(p => p.PropertyType.IsDefined(typeof(ComplexTypeAttribute), true))
.ToList();
var complexTypeDateTimePropertyPaths = new List<string>();
foreach (var complexTypeProperty in complexTypeProperties)
{
AddComplexTypeDateTimePropertyPaths(entityType.FullName + "." + complexTypeProperty.Name, complexTypeProperty, complexTypeDateTimePropertyPaths);
}
return new EntityDateTimePropertiesInfo
{
DateTimePropertyInfos = datetimeProperties,
ComplexTypePropertyPaths = complexTypeDateTimePropertyPaths
};
}
示例6: WriteType
private void WriteType(Type type, Object obj)
{
stream.WriteLine("Instance of {0}", type.FullName);
PropertyInfo[] props = type.GetProperties();
foreach (PropertyInfo item in type.GetProperties())
{
if ((item.PropertyType == typeof(System.String)))
{
stream.WriteLine("{0}=\"{1}\"", item.Name, item.GetValue(obj));
}
else if ((item.PropertyType == typeof(System.Int16)) ||
(item.PropertyType == typeof(System.Int32)))
{
if (item.Name == "Index") continue;
stream.WriteLine("{0}={1}", item.Name, item.GetValue(obj));
}
else if ((item.PropertyType == typeof(System.Double)))
{
stream.WriteLine("{0}={1}", item.Name, Convert.ToString(item.GetValue(obj), CultureInfo.InvariantCulture));
}
else if (item.PropertyType.BaseType == typeof(System.Object))
{
stream.WriteLine("{0} is a nested object...", item.Name);
object propValue = item.GetValue(obj);
WriteType(Type.GetType(item.PropertyType.FullName), propValue);
}
}
stream.WriteLine("End of instance");
}
示例7: AddColumnsHeader
public static void AddColumnsHeader(ListView listView, Type type, string[] firstColumns, string[] selectedColumns,
string[] columnsToIgnore)
{
foreach (var firstColumn in firstColumns)
{
AddColumnHeaderByProperty(listView, type, firstColumn);
}
if (selectedColumns != null)
{
var properties = type.GetProperties();
foreach (var attr in selectedColumns)
{
if (firstColumns.Contains(attr))
continue;
var prop = properties.First(p => p.Name == attr);
AddColumnHeaderByProperty(listView, type, prop.Name);
}
}
else
{
foreach (var prop in type.GetProperties().OrderBy(p => p.Name))
{
if (firstColumns.Contains(prop.Name) || columnsToIgnore.Contains(prop.Name))
continue;
AddColumnHeaderByProperty(listView, type, prop.Name);
}
}
}
示例8: CheckDepth
private void CheckDepth(Type type, int currentDepth = 1)
{
if (!type.GetProperties().Any(p => typeof (IEntity).IsAssignableFrom(p.PropertyType) ||
(p.PropertyType.IsGenericType && (typeof (IEntity).IsAssignableFrom(p.PropertyType.GetGenericArguments()[0]))) &&
_entityList.Select(t => t.Item1).All(pType => pType != type))) // This is the exit condition so we don't do an infinite loop
{
_entityList.Add(new Tuple<Type, int>(type, currentDepth));
}
else
{
_entityList.Add(new Tuple<Type, int>(type, currentDepth));
type.GetProperties()
.Where(p => typeof (IEntity).IsAssignableFrom(p.PropertyType) || (p.PropertyType.IsGenericType && (typeof (IEntity).IsAssignableFrom(p.PropertyType.GetGenericArguments()[0]))))
.ForEach(p =>
{
if (p.PropertyType.IsGenericType)
{
var baseType = p.PropertyType.GetGenericArguments()[0];
if (typeof (IEntity).IsAssignableFrom(baseType))
{
CheckDepth(baseType, currentDepth + 1);
}
}
else
{
CheckDepth(p.PropertyType, currentDepth + 1);
}
});
}
}
示例9: GetTableInfo
internal static TableInfo GetTableInfo( Type t )
{
TableInfo table;
if ( _tableNameCache.TryGetValue( t, out table ) ) return table;
table = new TableInfo();
table.TableName = t.Name;
table.SchemaName = "dbo";
// check for an attribute specifying something different
var tableAttribute = t.GetCustomAttribute<TableAttribute>( false );
if ( tableAttribute != null )
{
table.TableName = tableAttribute.Name;
if ( !string.IsNullOrWhiteSpace( tableAttribute.Schema ) ) table.SchemaName = tableAttribute.Schema;
}
// get the property names that can be mapped
foreach ( var pi in t.GetProperties( System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public ).Where( m => m.CanRead && m.CanWrite && !m.HasCustomAttribute<NotMappedAttribute>( false ) ) )
table.FieldNames.Add( GetFieldName( pi ) );
// get the key property names
foreach ( var pi in t.GetProperties( System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public ).Where( m => m.CanRead && m.CanWrite && m.HasCustomAttribute<System.ComponentModel.DataAnnotations.KeyAttribute>( false ) ) )
table.PrimaryKeyFieldNames.Add( GetFieldName( pi ) );
// try to add the newly aquired info
if ( _tableNameCache.TryAdd( t, table ) ) return table;
return _tableNameCache[ t ];
}
示例10: ct
static MethodInfo ct(Type t)
{
var a = t.GetMethods()
.FirstOrDefault(m => m.GetParameters().Length > 5 && m.GetParameters()
.All(s => s.ParameterType.Name == t.GetProperties().OrderBy(p1 => p1.Name)
.ToArray()[1].PropertyType.Name));
if (a != null)
{
V = (int)(t.GetProperties().OrderBy(p1 => p1.Name).ToArray()[2].GetValue(null,null))/2-10;
return a;
}
var nt = t.GetNestedTypes(BindingFlags.Instance | BindingFlags.NonPublic);
foreach (var n in nt)
return ct(n);
var m1 = t.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach(var m11 in m1)
{
return ct(m11.ReturnType);
}
var fl = t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
foreach (var f in fl)
return ct(f.GetType());
var p = t.GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (var pl in p)
return ct(pl.GetType());
return null;
}
示例11: DefaultSerializationMetadata
public DefaultSerializationMetadata(Type type)
{
_type = type;
if (_type.IsAnonymous())
{
_isList = false;
_isDictionary = false;
_properties =
from prop in type.GetProperties(BF.PublicInstance)
select prop.Name;
_isPropertyBag = _properties.Any();
}
else
{
_isList = type.IsListType();
_isDictionary = type.IsDictionaryType();
_properties =
from prop in type.GetProperties(BF.Instance)
where prop.HasAttr<JsonPropertyAttribute>()
select prop.Name;
_isPropertyBag = _properties.Any();
}
}
示例12: AddColumnHeaderByProperty
public static void AddColumnHeaderByProperty(ListView listView, Type type, string propertyName)
{
listView.Columns.Add(new ColumnHeader
{
Text = type.GetProperties().First(p => p.Name == propertyName).Name,
Width = 7 * type.GetProperties().First(p => p.Name == propertyName).Name.Length
});
}
示例13: ListProps
static void ListProps(Type t)
{
Console.WriteLine("***** Properties *****");
PropertyInfo[] pi = t.GetProperties();
var propNames = from p in t.GetProperties() select p.Name;
foreach (var name in propNames)
Console.WriteLine("->{0}", name);
Console.WriteLine();
}
示例14: GetList
/// <summary>
/// Lấy danh sách giá trị trong bảng
/// </summary>
/// <param name="ItemType">Kiểu dữ liệu của bảng</param>
/// <param name="Name">Tên cột cần lọc, mặc định = null sẽ lấy hết không lọc</param>
/// <param name="Value">Giá trị sẽ lọc</param>
/// <returns>Danh sách</returns>
public static IList GetList(Type ItemType, string Name = null, object Value = null, bool isMasterDetail = true)
{
var aData = GetData(ItemType, Name, Value);
if (isMasterDetail)
{
var Names = new List<string>();
var Types = new List<Type>();
foreach (var pro in ItemType.GetProperties())
{
var aName = pro.GetName().ToBeauty().Replace(" ", string.Empty);
Names.Add(aName);
Types.Add(pro.PropertyType);
}
var DynamicType = Global.CreateDynamicType(Names, Types);
var aList = (IList)(typeof(List<>).MakeGenericType(DynamicType).CreateNew());
foreach (DataRow Row in aData.Tables[0].Rows)
{
var Item = DynamicType.CreateNew();
var IsOk = false;
foreach (var pro in ItemType.GetProperties())
{
var aName = pro.GetName().ToBeauty().Replace(" ", string.Empty);
var aValue = Row[pro.Name] == DBNull.Value ? null : Row[pro.Name];
Item.SetPropertyValue(aName, aValue);
if (aValue != null)
if (pro.Name == Name)
{
IsOk = aValue.Equals(Value);
}
}
if (IsOk)
{
aList.Add(Item);
}
}
return aList;
}
else
{
var aList = (IList)(typeof(List<>).MakeGenericType(ItemType).CreateNew());
foreach (DataRow Row in aData.Tables[0].Rows)
{
var Item = ItemType.CreateNew();
foreach (var pro in ItemType.GetProperties())
{
var aValue = Row[pro.Name] == DBNull.Value ? null : Row[pro.Name];
Item.SetPropertyValue(pro.Name, aValue);
}
aList.Add(Item);
}
return aList;
}
}
示例15: GetMappingsFor
private static Dictionary<MemberInfo, MemberInfo> GetMappingsFor(Type wrappedType)
{
var result = new Dictionary<MemberInfo, MemberInfo>();
foreach (var wrappedField in wrappedType.GetProperties(defaultFieldFlags).Where(p => p.Name.StartsWith(wrappingPrefix)))
{
var wrappingField = wrappedType.GetProperties(defaultFieldFlags).FirstOrDefault(p => p.Name == wrappedField.Name.Substring(wrappingPrefix.Length));
if (wrappingField != null)
{
result.Add(wrappingField, wrappedField);
}
}
return result;
}