本文整理汇总了C#中System.Reflection.PropertyInfo.FirstAttribute方法的典型用法代码示例。如果您正苦于以下问题:C# PropertyInfo.FirstAttribute方法的具体用法?C# PropertyInfo.FirstAttribute怎么用?C# PropertyInfo.FirstAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.PropertyInfo
的用法示例。
在下文中一共展示了PropertyInfo.FirstAttribute方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetColumnDefinition
public static ColumnDefinition GetColumnDefinition(Type modelType, PropertyInfo propertyInfo, string columnName, string tableName)
{
var definition = new ColumnDefinition{ Name = columnName, TableName = tableName, ModificationType = ModificationType.Create };
//Look for specific Null setting attributed a column
var nullSettingAttribute = propertyInfo.FirstAttribute<NullSettingAttribute>();
if (nullSettingAttribute != null)
{
definition.IsNullable = nullSettingAttribute.NullSetting == NullSettings.Null;
}
//Look for specific DbType attributed a column
var databaseTypeAttribute = propertyInfo.FirstAttribute<SpecialDbTypeAttribute>();
if (databaseTypeAttribute != null)
{
definition.HasSpecialDbType = true;
definition.DbType = databaseTypeAttribute.DatabaseType;
}
else
{
definition.PropertyType = propertyInfo.PropertyType;
}
//Look for Primary Key for the current column
var primaryKeyColumnAttribute = propertyInfo.FirstAttribute<PrimaryKeyColumnAttribute>();
if (primaryKeyColumnAttribute != null)
{
string primaryKeyName = string.IsNullOrEmpty(primaryKeyColumnAttribute.Name)
? string.Format("PK_{0}", tableName)
: primaryKeyColumnAttribute.Name;
definition.IsPrimaryKey = true;
definition.IsIdentity = primaryKeyColumnAttribute.AutoIncrement;
definition.IsIndexed = primaryKeyColumnAttribute.Clustered;
definition.PrimaryKeyName = primaryKeyName;
definition.PrimaryKeyColumns = primaryKeyColumnAttribute.OnColumns ?? string.Empty;
definition.Seeding = primaryKeyColumnAttribute.IdentitySeed;
}
//Look for Size/Length of DbType
var lengthAttribute = propertyInfo.FirstAttribute<LengthAttribute>();
if (lengthAttribute != null)
{
definition.Size = lengthAttribute.Length;
}
//Look for Constraint for the current column
var constraintAttribute = propertyInfo.FirstAttribute<ConstraintAttribute>();
if (constraintAttribute != null)
{
definition.ConstraintName = constraintAttribute.Name ?? string.Empty;
definition.DefaultValue = constraintAttribute.Default ?? string.Empty;
}
return definition;
}
示例2: GetModelPropertyName
private static string GetModelPropertyName(PropertyInfo prop)
{
var dataMemberAttr = prop.FirstAttribute<DataMemberAttribute>();
if (dataMemberAttr != null && !dataMemberAttr.Name.IsNullOrEmpty())
return dataMemberAttr.Name;
return UseCamelCaseModelPropertyNames
? (UseLowercaseUnderscoreModelPropertyNames ? prop.Name.ToLowercaseUnderscore() : prop.Name.ToCamelCase())
: prop.Name;
}
示例3: ToProperty
public MetadataPropertyType ToProperty(PropertyInfo pi, object instance = null)
{
var property = new MetadataPropertyType
{
Name = pi.Name,
Attributes = ToAttributes(pi.GetCustomAttributes(false)),
Type = pi.PropertyType.GetMetadataPropertyType(),
IsValueType = pi.PropertyType.IsValueType() ? true : (bool?)null,
IsSystemType = pi.PropertyType.IsSystemType() ? true : (bool?)null,
IsEnum = pi.PropertyType.IsEnum() ? true : (bool?)null,
TypeNamespace = pi.PropertyType.Namespace,
DataMember = ToDataMember(pi.GetDataMember()),
GenericArgs = pi.PropertyType.IsGenericType()
? pi.PropertyType.GetGenericArguments().Select(x => x.ExpandTypeName()).ToArray()
: null,
Description = pi.GetDescription(),
};
var apiMember = pi.FirstAttribute<ApiMemberAttribute>();
if (apiMember != null)
{
if (apiMember.IsRequired)
property.IsRequired = true;
property.ParamType = apiMember.ParameterType;
property.DisplayType = apiMember.DataType;
}
var apiAllowableValues = pi.FirstAttribute<ApiAllowableValuesAttribute>();
if (apiAllowableValues != null)
{
property.AllowableValues = apiAllowableValues.Values;
property.AllowableMin = apiAllowableValues.Min;
property.AllowableMax = apiAllowableValues.Max;
}
if (instance != null)
{
var value = pi.GetValue(instance, null);
if (value != null
&& !value.Equals(pi.PropertyType.GetDefaultValue()))
{
if (pi.PropertyType.IsEnum())
{
property.Value = "{0}.{1}".Fmt(pi.PropertyType.Name, value);
}
else if (pi.PropertyType == typeof(Type))
{
var type = (Type)value;
property.Value = $"typeof({type.FullName})";
}
else
{
var strValue = value as string;
property.Value = strValue ?? value.ToJson();
}
}
if (pi.GetSetMethod() == null) //ReadOnly is bool? to minimize serialization
property.ReadOnly = true;
}
return property;
}
示例4: GetColumnDefinition
public static ColumnDefinition GetColumnDefinition(Type modelType, PropertyInfo propertyInfo, string columnName, string tableName)
{
var definition = new ColumnDefinition{ Name = columnName, TableName = tableName, ModificationType = ModificationType.Create };
//Look for specific Null setting attributed a column
var nullSettingAttribute = propertyInfo.FirstAttribute<NullSettingAttribute>();
if (nullSettingAttribute != null)
{
definition.IsNullable = nullSettingAttribute.NullSetting == NullSettings.Null;
}
//Look for specific DbType attributed a column
var databaseTypeAttribute = propertyInfo.FirstAttribute<SpecialDbTypeAttribute>();
if (databaseTypeAttribute != null)
{
definition.HasSpecialDbType = true;
definition.DbType = databaseTypeAttribute.DatabaseType;
}
else
{
definition.PropertyType = propertyInfo.PropertyType;
}
//Look for Primary Key for the current column
var primaryKeyColumnAttribute = propertyInfo.FirstAttribute<PrimaryKeyColumnAttribute>();
if (primaryKeyColumnAttribute != null)
{
string primaryKeyName = string.IsNullOrEmpty(primaryKeyColumnAttribute.Name)
? string.Format("PK_{0}", tableName)
: primaryKeyColumnAttribute.Name;
definition.IsPrimaryKey = true;
definition.IsIdentity = primaryKeyColumnAttribute.AutoIncrement;
definition.IsIndexed = primaryKeyColumnAttribute.Clustered;
definition.PrimaryKeyName = primaryKeyName;
definition.PrimaryKeyColumns = primaryKeyColumnAttribute.OnColumns ?? string.Empty;
definition.Seeding = primaryKeyColumnAttribute.IdentitySeed;
}
//Look for Size/Length of DbType
var lengthAttribute = propertyInfo.FirstAttribute<LengthAttribute>();
if (lengthAttribute != null)
{
definition.Size = lengthAttribute.Length;
}
//Look for Constraint for the current column
var constraintAttribute = propertyInfo.FirstAttribute<ConstraintAttribute>();
if (constraintAttribute != null)
{
//Special case for MySQL as it can't have multiple default DateTime values, which
//is what the umbracoServer table definition is trying to create
if (SqlSyntaxContext.SqlSyntaxProvider is MySqlSyntaxProvider && definition.TableName == "umbracoServer" &&
definition.TableName.ToLowerInvariant() == "lastNotifiedDate".ToLowerInvariant())
return definition;
definition.ConstraintName = constraintAttribute.Name ?? string.Empty;
definition.DefaultValue = constraintAttribute.Default ?? string.Empty;
}
return definition;
}