本文整理汇总了C#中System.Reflection.PropertyInfo.GetCodeFirstAttribute方法的典型用法代码示例。如果您正苦于以下问题:C# PropertyInfo.GetCodeFirstAttribute方法的具体用法?C# PropertyInfo.GetCodeFirstAttribute怎么用?C# PropertyInfo.GetCodeFirstAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.PropertyInfo
的用法示例。
在下文中一共展示了PropertyInfo.GetCodeFirstAttribute方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateProperty
/// <summary>
/// Creates a new property on the ContentType under the correct tab
/// </summary>
/// <param name="newContentType"></param>
/// <param name="tabName"></param>
/// <param name="dataTypeService"></param>
/// <param name="atTabGeneric"></param>
/// <param name="item"></param>
public PropertyRegistration CreateProperty(IContentTypeBase newContentType, TabRegistration tab, PropertyInfo item, Type documentClrType)
{
ContentPropertyAttribute attribute = item.GetCodeFirstAttribute<ContentPropertyAttribute>();
var tabPostfix = tab == null || !attribute.AddTabAliasToPropertyAlias ? null : tab.OriginalName == null ? tab.Name : tab.OriginalName;
var dataType = _dataTypeModule.GetDataType(item);
var property = new PropertyRegistration();
property.Name = attribute.Name;
property.Alias = tabPostfix == null ? attribute.Alias : StringHelperExtensions.HyphenToUnderscore(StringHelperExtensions.ParseUrl(attribute.Alias + "_" + tabPostfix, false));
property.DataType = dataType;
property.PropertyAttribute = attribute;
property.Metadata = item;
PropertyType propertyType = new PropertyType(dataType.Definition);
propertyType.Name = property.Name;
propertyType.Alias = property.Alias;
propertyType.Description = attribute.Description;
propertyType.Mandatory = attribute.Mandatory;
propertyType.SortOrder = attribute.SortOrder;
propertyType.ValidationRegExp = attribute.ValidationRegularExpression;
var propertyDeclaredOnThisDocType = property.Metadata.DeclaringType == documentClrType || property.Metadata.DeclaringType.GetCodeFirstAttribute<CodeFirstCommonBaseAttribute>(false) != null;
var propertyDeclaredOnThisTab = tab == null ? false : property.Metadata.DeclaringType == tab.ClrType;
var tabDeclaredOnThisDocType = tab == null ? false : tab.PropertyOfParent.DeclaringType == documentClrType || tab.PropertyOfParent.DeclaringType.GetCodeFirstAttribute<CodeFirstCommonBaseAttribute>() != null;
var declaringTypeIsDocType = property.Metadata.DeclaringType.GetCodeFirstAttribute<ContentTypeAttribute>(false) != null;
var propertyIsFromCommonBase = tab == null ?
property.Metadata.DeclaringType.GetCodeFirstAttribute<CodeFirstCommonBaseAttribute>() != null && property.Metadata.DeclaringType == documentClrType.BaseType
:
property.Metadata.DeclaringType.GetCodeFirstAttribute<CodeFirstCommonBaseAttribute>() != null && property.Metadata.DeclaringType == tab.ClrType.BaseType;
if (tab == null)
{
if (propertyDeclaredOnThisDocType || propertyIsFromCommonBase) //only if property declared at this level (or inherited from a non-doctype class)
{
if (!propertyIsFromCommonBase || !newContentType.PropertyTypeExists(propertyType.Alias)) //check if common base properties already exist
{
CodeFirstManager.Current.Log("Adding property " + property.Name + " on content type " + newContentType.Name, this);
newContentType.AddPropertyType(propertyType);
}
}
}
else if (tabDeclaredOnThisDocType || propertyIsFromCommonBase) //only if tab declared at this level
{
if (propertyDeclaredOnThisTab || propertyIsFromCommonBase) //only if property declared at this level
{
if (!propertyIsFromCommonBase || !newContentType.PropertyTypeExists(propertyType.Alias)) //check if common base properties already exist
{
CodeFirstManager.Current.Log("Adding property " + property.Name + " on tab " + tab.Name + " of content type " + newContentType.Name, this);
newContentType.AddPropertyType(propertyType, tab.Name);
}
}
}
return property;
}
示例2: VerifyExistingProperty
/// <summary>
/// Checks whether a property exists and adds if if it does not. The data type, alias, description and mandatory flag are update for existing properties, but not persisted.
/// Callers should persist the value.
/// </summary>
public PropertyRegistration VerifyExistingProperty(IContentTypeBase contentType, TabRegistration tab, PropertyInfo item, Type documentClrType, ref bool modified)
{
ContentPropertyAttribute attribute = item.GetCodeFirstAttribute<ContentPropertyAttribute>();
var tabPostfix = tab == null || !attribute.AddTabAliasToPropertyAlias ? null : tab.OriginalName == null ? tab.Name : tab.OriginalName;
var property = new PropertyRegistration();
var alias = property.Alias = tabPostfix == null ? attribute.Alias : StringHelperExtensions.HyphenToUnderscore(StringHelperExtensions.ParseUrl(attribute.Alias + "_" + tabPostfix, false));
var dataType = property.DataType = _dataTypeModule.GetDataType(item);
property.Name = attribute.Name;
property.PropertyAttribute = attribute;
property.Metadata = item;
LogPropertySyncInfo(contentType, tab, property, "Syncing");
bool alreadyExisted = contentType.PropertyTypeExists(alias);
PropertyType umbracoProperty = contentType.PropertyTypes.FirstOrDefault(x => x.Alias == alias);
if (umbracoProperty == null && alreadyExisted)
{
//This is a property from an underlying tab. Leave it alone. Log a warning in case this is an orphaned property.
LogPropertySyncInfo(contentType, tab, property, "Ignoring inherited");
return property;
}
if(alreadyExisted)
{
modified = modified ||
!umbracoProperty.Name.Equals(attribute.Name, StringComparison.InvariantCultureIgnoreCase) ||
umbracoProperty.Mandatory != attribute.Mandatory ||
(umbracoProperty.SortOrder != attribute.SortOrder && attribute.SortOrder != 0); //don't count sort order changes if no sort order is specified, as Umbraco will have assigned an automatic one
if (umbracoProperty.ValidationRegExp != attribute.ValidationRegularExpression)
{
//If not both null/empty
if (!(string.IsNullOrEmpty(umbracoProperty.ValidationRegExp) && string.IsNullOrEmpty(attribute.ValidationRegularExpression)))
{
modified = true;
LogPropertySyncInfo(contentType, tab, property, "ValidationRegExp changed on");
}
}
if (umbracoProperty.Description != attribute.Description)
{
//If not both null/empty
if (!(string.IsNullOrEmpty(umbracoProperty.Description) && string.IsNullOrEmpty(attribute.Description)))
{
modified = true;
LogPropertySyncInfo(contentType, tab, property, "Description changed on");
}
}
if (modified)
{
if (!umbracoProperty.Name.Equals(attribute.Name, StringComparison.InvariantCultureIgnoreCase))
LogPropertySyncInfo(contentType, tab, property, "Name changed on");
if (umbracoProperty.Mandatory != attribute.Mandatory)
LogPropertySyncInfo(contentType, tab, property, "Mandatory changed on");
if ((umbracoProperty.SortOrder != attribute.SortOrder && attribute.SortOrder != 0))
LogPropertySyncInfo(contentType, tab, property, "SortOrder changed on");
}
}
if (umbracoProperty == null)
{
try
{
modified = true;
umbracoProperty = new PropertyType(dataType.Definition);
LogPropertySyncInfo(contentType, tab, property, "Creating new");
}
catch (Exception ex)
{
}
}
else if (umbracoProperty.DataTypeDefinitionId != dataType.Definition.Id)
{
modified = true;
umbracoProperty.DataTypeDefinitionId = dataType.Definition.Id;
LogPropertySyncInfo(contentType, tab, property, "Data type changed for");
}
umbracoProperty.Name = attribute.Name;
umbracoProperty.Alias = alias;
umbracoProperty.Description = attribute.Description;
umbracoProperty.Mandatory = attribute.Mandatory;
umbracoProperty.SortOrder = attribute.SortOrder;
umbracoProperty.ValidationRegExp = attribute.ValidationRegularExpression;
var propertyDeclaredOnThisDocType = property.Metadata.DeclaringType == documentClrType;
var propertyDeclaredOnThisTab = tab == null ? false : property.Metadata.DeclaringType == tab.ClrType;
var tabDeclaredOnThisDocType = tab == null ? false : tab.PropertyOfParent.DeclaringType == documentClrType || tab.PropertyOfParent.DeclaringType.GetCustomAttribute<ContentTypeAttribute>() == null;
var declaringTypeIsDocType = property.Metadata.DeclaringType.GetCodeFirstAttribute<ContentTypeAttribute>(false) != null;
var propertyIsFromCommonBase = tab == null ?
property.Metadata.DeclaringType.GetCodeFirstAttribute<CodeFirstCommonBaseAttribute>() != null && property.Metadata.DeclaringType == documentClrType.BaseType
//.........这里部分代码省略.........