本文整理汇总了C#中System.ComponentModel.PropertyDescriptor.ExplicitAttributes方法的典型用法代码示例。如果您正苦于以下问题:C# PropertyDescriptor.ExplicitAttributes方法的具体用法?C# PropertyDescriptor.ExplicitAttributes怎么用?C# PropertyDescriptor.ExplicitAttributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ComponentModel.PropertyDescriptor
的用法示例。
在下文中一共展示了PropertyDescriptor.ExplicitAttributes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TypePropertyMetadata
public TypePropertyMetadata(PropertyDescriptor descriptor)
{
_propertyDescriptor = descriptor;
ValidationRules = new List<TypePropertyValidationRuleMetadata>();
var elementType = TypeUtility.GetElementType(descriptor.PropertyType);
IsArray = !(elementType == descriptor.PropertyType);
var propertyAttributes = descriptor.ExplicitAttributes();
IsKey = null != propertyAttributes[typeof (KeyAttribute)];
// TODO, 336102, ReadOnlyAttribute for editability? RIA used EditableAttribute?
IsReadOnly = propertyAttributes.OfType<ReadOnlyAttribute>().Any(a => a.IsReadOnly);
var associationAttr = propertyAttributes.OfType<AssociationAttribute>().SingleOrDefault();
if (associationAttr != null)
Association = new TypePropertyAssociationMetadata(associationAttr);
var requiredAttribute = propertyAttributes.OfType<RequiredAttribute>().SingleOrDefault();
if (requiredAttribute != null)
ValidationRules.Add(new TypePropertyValidationRuleMetadata(requiredAttribute));
#region Validation Rules
var rangeAttribute = (RangeAttribute) propertyAttributes[typeof (RangeAttribute)];
if (rangeAttribute != null) {
var operandType = rangeAttribute.OperandType;
operandType = Nullable.GetUnderlyingType(operandType) ?? operandType;
if (operandType == typeof (Double)
|| operandType == typeof (Int16)
|| operandType == typeof (Int32)
|| operandType == typeof (Int64)
|| operandType == typeof (Single))
ValidationRules.Add(new TypePropertyValidationRuleMetadata(rangeAttribute));
}
var stringLengthAttribute = (StringLengthAttribute) propertyAttributes[typeof (StringLengthAttribute)];
if (stringLengthAttribute != null)
ValidationRules.Add(new TypePropertyValidationRuleMetadata(stringLengthAttribute));
var dataTypeAttribute = (DataTypeAttribute) propertyAttributes[typeof (DataTypeAttribute)];
if (dataTypeAttribute != null) {
if (dataTypeAttribute.DataType.Equals(DataType.EmailAddress)
|| dataTypeAttribute.DataType.Equals(DataType.Url))
ValidationRules.Add(new TypePropertyValidationRuleMetadata(dataTypeAttribute));
}
#endregion
}
示例2: TypePropertyMetadata
/// <summary>
/// Initializes a new instance of the <see cref="TypePropertyMetadata" /> class.
/// </summary>
/// <param name="descriptor">The descriptor.</param>
public TypePropertyMetadata(PropertyDescriptor descriptor)
{
this.Name = descriptor.Name;
Type elementType = TypeUtility.GetElementType(descriptor.PropertyType);
this.IsArray = !elementType.Equals(descriptor.PropertyType);
this.TypeName = elementType.Name;
this.TypeNamespace = elementType.Namespace;
AttributeCollection attributeCollection = descriptor.ExplicitAttributes();
ReadOnlyAttribute readOnlyAttribute = (ReadOnlyAttribute)attributeCollection[typeof(ReadOnlyAttribute)];
this.IsReadOnly = (readOnlyAttribute != null && readOnlyAttribute.IsReadOnly);
AssociationAttribute associationAttribute = (AssociationAttribute)attributeCollection[typeof(AssociationAttribute)];
if (associationAttribute != null)
{
this.Association = new MetadataGenerator.TypePropertyAssociationMetadata(associationAttribute);
}
RequiredAttribute requiredAttribute = (RequiredAttribute)attributeCollection[typeof(RequiredAttribute)];
if (requiredAttribute != null)
{
this.validationRules.Add(new MetadataGenerator.TypePropertyValidationRuleMetadata(requiredAttribute));
}
RangeAttribute rangeAttribute = (RangeAttribute)attributeCollection[typeof(RangeAttribute)];
if (rangeAttribute != null)
{
Type type = rangeAttribute.OperandType;
type = (Nullable.GetUnderlyingType(type) ?? type);
if (type.Equals(typeof(double)) || type.Equals(typeof(short)) || type.Equals(typeof(int)) || type.Equals(typeof(long)) || type.Equals(typeof(float)))
{
this.validationRules.Add(new MetadataGenerator.TypePropertyValidationRuleMetadata(rangeAttribute));
}
}
StringLengthAttribute stringLengthAttribute = (StringLengthAttribute)attributeCollection[typeof(StringLengthAttribute)];
if (stringLengthAttribute != null)
{
this.validationRules.Add(new MetadataGenerator.TypePropertyValidationRuleMetadata(stringLengthAttribute));
}
DataTypeAttribute dataTypeAttribute = (DataTypeAttribute)attributeCollection[typeof(DataTypeAttribute)];
if (dataTypeAttribute != null && (dataTypeAttribute.DataType.Equals(DataType.EmailAddress) || dataTypeAttribute.DataType.Equals(DataType.Url)))
{
this.validationRules.Add(new MetadataGenerator.TypePropertyValidationRuleMetadata(dataTypeAttribute));
}
}
示例3: CanGenerateProperty
internal override bool CanGenerateProperty(PropertyDescriptor propertyDescriptor)
{
// Check if it is an excluded property
if (propertyDescriptor.Attributes[typeof(ExcludeAttribute)] != null)
{
return false;
}
// Check if it is an external reference.
if (propertyDescriptor.Attributes[typeof(ExternalReferenceAttribute)] != null)
{
return true;
}
// The base can't generate the property, it could be an association which we know how to generate.
if (!base.CanGenerateProperty(propertyDescriptor))
{
AttributeCollection propertyAttributes = propertyDescriptor.ExplicitAttributes();
bool hasKeyAttr = (propertyAttributes[typeof(KeyAttribute)] != null);
// If we can't generate Key property, log a VS error (this will cancel CodeGen effectively)
if (hasKeyAttr)
{
// Property must not be serializable based on attributes (e.g. no DataMember), because
// we already checked its type which was fine.
this.ClientCodeGenerator.CodeGenerationHost.LogError(string.Format(
CultureInfo.CurrentCulture,
Resource.EntityCodeGen_EntityKey_PropertyNotSerializable,
this.Type, propertyDescriptor.Name));
return false;
}
// Get the implied element type (e.g. int32[], Nullable<int32>, IEnumerable<int32>)
// If the ultimate element type is not allowed, it's not acceptable, no matter whether
// this is an array, Nullable<T> or whatever
Type elementType = TypeUtility.GetElementType(propertyDescriptor.PropertyType);
if (!this._domainServiceDescriptionAggregate.EntityTypes.Contains(elementType) || (propertyDescriptor.Attributes[typeof(AssociationAttribute)] == null))
{
// If the base class says we can't generate the property, it is because the property is not serializable.
// The only other type entity would serialize is associations. Since it is not, return now.
return false;
}
}
// Ensure the property is not virtual, abstract or new
// If there is a violation, we log the error and keep
// running to accumulate all such errors. This function
// may return an "okay" for non-error case polymorphics.
if (!this.CanGeneratePropertyIfPolymorphic(propertyDescriptor))
{
return false;
}
return true;
}
示例4: OnPropertySkipped
internal override void OnPropertySkipped(PropertyDescriptor pd)
{
AttributeCollection propertyAttributes = pd.ExplicitAttributes();
bool hasKeyAttr = (propertyAttributes[typeof(KeyAttribute)] != null);
// If we can't generate Key property, log a VS error (this will cancel CodeGen effectively)
if (hasKeyAttr)
{
// Property must not be serializable based on attributes (e.g. no DataMember), because
// we already checked its type which was fine.
this.ClientCodeGenerator.CodeGenerationHost.LogError(string.Format(
CultureInfo.CurrentCulture,
Resource.EntityCodeGen_EntityKey_PropertyNotSerializable,
this.Type, pd.Name));
}
}
示例5: IsPropertyShared
internal override bool IsPropertyShared(PropertyDescriptor pd)
{
if (base.IsPropertyShared(pd))
{
if (pd.ExplicitAttributes()[typeof(KeyAttribute)] != null)
{
// If there are any shared key members, don't generate GetIdentity method.
// Default implementation of GetIdentity on the Entity class will be used.
this.GenerateGetIdentity = false;
}
return true;
}
return false;
}
示例6: HandleNonSerializableProperty
internal override bool HandleNonSerializableProperty(PropertyDescriptor propertyDescriptor)
{
AttributeCollection propertyAttributes = propertyDescriptor.ExplicitAttributes();
AssociationAttribute associationAttr = (AssociationAttribute)propertyAttributes[typeof(AssociationAttribute)];
bool externalReference = propertyAttributes[typeof(ExternalReferenceAttribute)] != null;
if (associationAttr != null)
{
this.AddAssociationToGenerate(propertyDescriptor);
return true;
}
return false;
}
示例7: ShouldDeclareProperty
internal override bool ShouldDeclareProperty(PropertyDescriptor pd)
{
if (!base.ShouldDeclareProperty(pd))
{
return false;
}
// Inheritance: when dealing with derived entities, we need to
// avoid generating a property already on the base. But we also
// need to account for flattening (holes in the exposed hiearchy.
// This helper method encapsulates that logic.
if (!this.ShouldFlattenProperty(pd))
{
return false;
}
AttributeCollection propertyAttributes = pd.ExplicitAttributes();
Type propertyType = pd.PropertyType;
// The [Key] attribute means this property is part of entity key
bool hasKeyAttr = (propertyAttributes[typeof(KeyAttribute)] != null);
if (hasKeyAttr)
{
if (!TypeUtility.IsPredefinedSimpleType(propertyType))
{
this.ClientCodeGenerator.CodeGenerationHost.LogError(string.Format(
CultureInfo.CurrentCulture,
Resource.EntityCodeGen_EntityKey_KeyTypeNotSupported,
this.Type, pd.Name, propertyType));
return false;
}
}
return true;
}
示例8: ShouldDeclareProperty
internal virtual bool ShouldDeclareProperty(PropertyDescriptor pd)
{
AttributeCollection propertyAttributes = pd.ExplicitAttributes();
if (this.IsExcluded(pd, propertyAttributes))
{
// Ignore the [Include] because that's what we do during serialization as well. (We don't want to
// check for [Exclude] + [Include] everywhere in our code base.)
return false;
}
if (this.IsPropertyShared(pd))
{
return false;
}
return true;
}
示例9: GetPropertyAttributes
internal IEnumerable<Attribute> GetPropertyAttributes(PropertyDescriptor propertyDescriptor, Type propertyType)
{
List<Attribute> propertyAttributes = propertyDescriptor.ExplicitAttributes().Cast<Attribute>().ToList();
if (!propertyAttributes.OfType<DataMemberAttribute>().Any())
{
propertyAttributes.Add(new DataMemberAttribute());
}
ReadOnlyAttribute readOnlyAttr = propertyAttributes.OfType<ReadOnlyAttribute>().SingleOrDefault();
if (readOnlyAttr != null && !propertyAttributes.OfType<EditableAttribute>().Any())
{
propertyAttributes.Add(new EditableAttribute(!readOnlyAttr.IsReadOnly));
}
if (TypeUtility.IsSupportedComplexType(propertyType) && !propertyAttributes.OfType<DisplayAttribute>().Any())
{
DisplayAttribute displayAttribute = new DisplayAttribute() { AutoGenerateField = false };
propertyAttributes.Add(displayAttribute);
}
// If the data contract type already contains the RoundtripOriginalAttribute, then we remove the attribute from properties.
if (this.Type.Attributes()[typeof(RoundtripOriginalAttribute)] != null)
{
propertyAttributes.RemoveAll(attr => attr.GetType() == typeof(RoundtripOriginalAttribute));
}
return propertyAttributes;
}
示例10: GenerateProperty
/// <summary>
/// Generates a property getter/setter pair into the given proxy class to match the given property info.
/// </summary>
/// <param name="propertyDescriptor">PropertyDescriptor for the property to generate for.</param>
protected virtual void GenerateProperty(PropertyDescriptor propertyDescriptor)
{
string propertyName = propertyDescriptor.Name;
Type propertyType = CodeGenUtilities.TranslateType(propertyDescriptor.PropertyType);
// ----------------------------------------------------------------
// Property type ref
// ----------------------------------------------------------------
var propTypeReference = CodeGenUtilities.GetTypeReference(propertyType, this.ClientProxyGenerator, this.ProxyClass);
// ----------------------------------------------------------------
// Property decl
// ----------------------------------------------------------------
var property = new CodeMemberProperty();
property.Name = propertyName;
property.Type = propTypeReference;
property.Attributes = MemberAttributes.Public | MemberAttributes.Final; // final needed, else becomes virtual
List<Attribute> propertyAttributes = propertyDescriptor.ExplicitAttributes().Cast<Attribute>().ToList();
// Generate <summary> for property
string comment = string.Format(CultureInfo.CurrentCulture, Resource.CodeGen_Entity_Property_Summary_Comment, propertyName);
property.Comments.AddRange(CodeGenUtilities.GenerateSummaryCodeComment(comment, this.ClientProxyGenerator.IsCSharp));
// ----------------------------------------------------------------
// [DataMember] -> Add if not already present.
// ----------------------------------------------------------------
// Add if not already present.
if (!propertyAttributes.OfType<DataMemberAttribute>().Any())
{
CodeAttributeDeclaration dataMemberAtt = CodeGenUtilities.CreateAttributeDeclaration(typeof(DataMemberAttribute), this.ClientProxyGenerator, this.ProxyClass);
property.CustomAttributes.Add(dataMemberAtt);
}
// Here, we check for the existence of [ReadOnly(true)] attributes generated when
// the property does not not have a setter. We want to inject an [Editable(false)]
// attribute into the pipeline.
ReadOnlyAttribute readOnlyAttr = propertyAttributes.OfType<ReadOnlyAttribute>().SingleOrDefault();
if (readOnlyAttr != null && !propertyAttributes.OfType<EditableAttribute>().Any())
{
propertyAttributes.Add(new EditableAttribute(!readOnlyAttr.IsReadOnly));
// REVIEW: should we strip out [ReadOnly] attributes here?
}
// Here we check if the type has a RoundtripOriginalAttribute. In that case we strip it out from the property
if (this._isRoundtripType)
{
propertyAttributes.RemoveAll(attr => attr.GetType() == typeof(RoundtripOriginalAttribute));
}
// Here we check for database generated fields. In that case we strip any RequiredAttribute from the property.
if (propertyAttributes.Any(a=>a.GetType().Name == "DatabaseGeneratedAttribute"))
{
propertyAttributes.RemoveAll(attr => attr.GetType() == typeof (RequiredAttribute));
}
// Here, we check for the presence of a complex type. If it exists we need to add a DisplayAttribute
// if not already there. DataSources windows do not handle complex types
if (TypeUtility.IsSupportedComplexType(propertyType) && !propertyAttributes.OfType<DisplayAttribute>().Any())
{
CodeAttributeDeclaration displayAttribute = CodeGenUtilities.CreateDisplayAttributeDeclaration(this.ClientProxyGenerator, this.ProxyClass);
property.CustomAttributes.Add(displayAttribute);
}
// ----------------------------------------------------------------
// Propagate the custom attributes
// ----------------------------------------------------------------
CustomAttributeGenerator.GenerateCustomAttributes(
this.ClientProxyGenerator,
this.ProxyClass,
ex => string.Format(CultureInfo.CurrentCulture, Resource.ClientCodeGen_Attribute_ThrewException_CodeTypeMember, ex.Message, property.Name, this.ProxyClass.Name, ex.InnerException.Message),
propertyAttributes.Cast<Attribute>(),
property.CustomAttributes,
property.Comments);
// ----------------------------------------------------------------
// backing private field (CodeDom doesn't yet know about auto properties)
// ----------------------------------------------------------------
string fieldName = CodeGenUtilities.MakeCompliantFieldName(propertyName);
var field = new CodeMemberField(propTypeReference, fieldName);
this.ProxyClass.Members.Add(field);
var fieldRef = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fieldName);
var valueRef = new CodePropertySetValueReferenceExpression();
// ----------------------------------------------------------------
// getter body
// ----------------------------------------------------------------
property.GetStatements.Add(new CodeMethodReturnStatement(fieldRef));
// ----------------------------------------------------------------
// setter body
// ----------------------------------------------------------------
List<CodeStatement> bodyStatements = new List<CodeStatement>();
//.........这里部分代码省略.........
示例11: AssociationMetadata
public AssociationMetadata(PropertyDescriptor pd)
{
this.PropertyDescriptor = pd;
AttributeCollection propertyAttributes = pd.ExplicitAttributes();
this.AssociationAttribute = (AssociationAttribute) propertyAttributes[typeof (AssociationAttribute)];
this.IsExternal = propertyAttributes[typeof (ExternalReferenceAttribute)] != null;
this.IsCollection = EntityGenerator.IsCollectionType(pd.PropertyType);
if (!this.IsCollection)
{
this.PropTypeName = CodeGenUtilities.GetTypeName(pd.PropertyType);
this.AssociationTypeName = @"OpenRiaServices.DomainServices.Client.EntityRef<" + this.PropTypeName + ">";
this.Attributes = propertyAttributes.Cast<Attribute>().Where(a => a.GetType() != typeof (DataMemberAttribute));
}
else
{
this.PropTypeName = CodeGenUtilities.GetTypeName(TypeUtility.GetElementType(pd.PropertyType));
this.AssociationTypeName = "OpenRiaServices.DomainServices.Client.EntityCollection<" + this.PropTypeName + ">";
List<Attribute> attributeList = propertyAttributes.Cast<Attribute>().ToList();
ReadOnlyAttribute readOnlyAttr = propertyAttributes.OfType<ReadOnlyAttribute>().SingleOrDefault();
if (readOnlyAttr != null && !propertyAttributes.OfType<EditableAttribute>().Any())
{
attributeList.Add(new EditableAttribute(!readOnlyAttr.IsReadOnly));
}
this.Attributes = attributeList.Where(a => a.GetType() != typeof (DataMemberAttribute));
}
this.PropertyName = CodeGenUtilities.GetSafeName(pd.Name);
this.FieldName = CodeGenUtilities.MakeCompliantFieldName(this.PropertyName);
}
示例12: GenerateSingletonAssociation
private void GenerateSingletonAssociation(CodeTypeDeclaration proxyClass, PropertyDescriptor pd, AssociationAttribute associationAttribute, bool isExternal)
{
CodeTypeReference propType =
isExternal ?
new CodeTypeReference(pd.PropertyType.FullName) { Options = CodeTypeReferenceOptions.GlobalReference } :
CodeGenUtilities.GetTypeReference(pd.PropertyType, this.ClientProxyGenerator, proxyClass);
// generate field:
// private EntityRef<Product> _Product;
CodeTypeReference fldType = CodeGenUtilities.GetTypeReference(TypeConstants.EntityRefTypeFullName, this.Type.Namespace, false);
fldType.TypeArguments.Add(propType);
CodeMemberField fld = new CodeMemberField();
fld.Attributes = MemberAttributes.Private;
fld.Name = CodeGenUtilities.MakeCompliantFieldName(pd.Name);
fld.Type = fldType;
proxyClass.Members.Add(fld);
// generate property:
// public Product Product { get {...} set {...} }
CodeMemberProperty prop = new CodeMemberProperty();
prop.Attributes = MemberAttributes.Public | MemberAttributes.Final;
prop.Name = pd.Name;
prop.Type = propType;
prop.HasGet = true;
prop.HasSet = true;
// For the FK side of external associations, we generate a "reduced" setter that
// does validation and synchronizes FK members
if (isExternal && !associationAttribute.IsForeignKey)
{
prop.HasSet = false;
}
// Generate <summary> comment for property
string format = prop.HasSet
? Resource.CodeGen_Entity_Singleton_Association_Property_Summary_Comment
: Resource.CodeGen_Entity_Singleton_Association_ReadOnly_Property_Summary_Comment;
string comment = string.Format(CultureInfo.CurrentCulture, format, pd.PropertyType.Name);
prop.Comments.AddRange(CodeGenUtilities.GenerateSummaryCodeComment(comment, this.ClientProxyGenerator.IsCSharp));
// ----------------------------------------------------------------
// Propagate the custom attributes (except DataMember)
// ----------------------------------------------------------------
AttributeCollection propertyAttributes = pd.ExplicitAttributes();
CustomAttributeGenerator.GenerateCustomAttributes(
this.ClientProxyGenerator,
proxyClass,
ex => string.Format(CultureInfo.CurrentCulture, Resource.ClientCodeGen_Attribute_ThrewException_CodeTypeMember, ex.Message, prop.Name, proxyClass.Name, ex.InnerException.Message),
propertyAttributes.Cast<Attribute>().Where(a => a.GetType() != typeof(DataMemberAttribute)),
prop.CustomAttributes,
prop.Comments);
// --------------------------
// Generate the filter method
// --------------------------
// private bool filter_Product(Product entity) {
// return entity.ProductID == ProductID;
// }
string[] thisKeyProps = associationAttribute.ThisKeyMembers.ToArray();
string[] otherKeyProps = associationAttribute.OtherKeyMembers.ToArray();
CodeMemberMethod filterMethod = this.GenerateFilterMethod(proxyClass, pd.Name, pd.PropertyType, thisKeyProps, otherKeyProps, isExternal);
// --------------------------
// Generate getter
// --------------------------
// generate delayed initialization
// if (_Product == null) {
// _Product = new EntityRef<Product>(this, filter_Product);
// }
CodeExpression entityExpr = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fld.Name);
entityExpr = new CodePropertyReferenceExpression(entityExpr, "Entity");
CodeExpression isRefNullExpr =
CodeGenUtilities.MakeEqualToNull(
new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fld.Name));
CodeExpression filterDelegate =
CodeGenUtilities.MakeDelegateCreateExpression(this.ClientProxyGenerator.IsCSharp, new CodeTypeReference("System.Func"), filterMethod.Name);
CodeAssignStatement initExpr = new CodeAssignStatement(
new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fld.Name),
new CodeObjectCreateExpression(
fldType,
new CodeThisReferenceExpression(),
new CodePrimitiveExpression(prop.Name),
filterDelegate));
prop.GetStatements.Add(new CodeConditionStatement(isRefNullExpr, initExpr));
// generate : return _Product.Entity;
prop.GetStatements.Add(new CodeMethodReturnStatement(entityExpr));
// --------------------------
// Generate setter
// --------------------------
if (prop.HasSet)
{
PropertyDescriptor reverseAssociationMember = GetReverseAssociation(pd, associationAttribute);
CodeStatement detachStatement = null;
CodeStatement attachStatement = null;
//.........这里部分代码省略.........
示例13: GenerateCollectionSideAssociation
private void GenerateCollectionSideAssociation(CodeTypeDeclaration proxyClass, PropertyDescriptor pd, AssociationAttribute associationAttribute, bool isExternal)
{
CodeTypeReference propType = CodeGenUtilities.GetTypeReference(TypeConstants.EntityCollectionTypeFullName, this.Type.Namespace, false);
CodeTypeReference fldType;
Type elementType = TypeUtility.GetElementType(pd.PropertyType);
if (isExternal)
{
CodeTypeReference externalTypeRef = new CodeTypeReference(elementType.FullName);
propType.TypeArguments.Add(externalTypeRef);
// Note: we set the global flag *after* adding as a TypeArgument
// to override the GenericTypeArgument flag. This is required
// for proper global references.
externalTypeRef.Options = CodeTypeReferenceOptions.GlobalReference;
}
else
{
propType.TypeArguments.Add(
CodeGenUtilities.GetTypeReference(
elementType,
this.ClientProxyGenerator,
proxyClass));
}
fldType = propType;
CodeMemberField fld = new CodeMemberField();
fld.Attributes = MemberAttributes.Private;
fld.Name = CodeGenUtilities.MakeCompliantFieldName(pd.Name);
fld.Type = fldType;
proxyClass.Members.Add(fld);
// --------------------------
// Generate the filter method
// --------------------------
// private bool filter_PurchaseOrderDetails(PurchaseOrderDetail entity) {
// return entity.ProductID == ProductID;
// }
string[] thisKeyProps = associationAttribute.ThisKeyMembers.ToArray();
string[] otherKeyProps = associationAttribute.OtherKeyMembers.ToArray();
CodeMemberMethod filterMethod = this.GenerateFilterMethod(proxyClass, pd.Name, elementType, thisKeyProps, otherKeyProps, isExternal);
CodeMemberProperty prop = new CodeMemberProperty();
prop.Name = pd.Name;
prop.Attributes = MemberAttributes.Public | MemberAttributes.Final;
prop.Type = propType;
prop.HasGet = true;
// Generate <summary> comment for property
string comment = string.Format(CultureInfo.CurrentCulture, Resource.CodeGen_Entity_Collection_Association_Property_Summary_Comment, elementType.Name);
prop.Comments.AddRange(CodeGenUtilities.GenerateSummaryCodeComment(comment, this.ClientProxyGenerator.IsCSharp));
// ----------------------------------------------------------------
// Propagate the custom attributes (except DataMember)
// ----------------------------------------------------------------
List<Attribute> propertyAttributes = pd.ExplicitAttributes().Cast<Attribute>().ToList();
// Here, we check for the existence of [ReadOnly(true)] attributes generated when
// the property does not not have a setter. We want to inject an [Editable(false)]
// attribute into the pipeline.
ReadOnlyAttribute readOnlyAttr = propertyAttributes.OfType<ReadOnlyAttribute>().SingleOrDefault();
if (readOnlyAttr != null && !propertyAttributes.OfType<EditableAttribute>().Any())
{
propertyAttributes.Add(new EditableAttribute(!readOnlyAttr.IsReadOnly));
// REVIEW: should we strip out [ReadOnly] attributes here?
}
CustomAttributeGenerator.GenerateCustomAttributes(
this.ClientProxyGenerator,
proxyClass,
ex => string.Format(CultureInfo.CurrentCulture, Resource.ClientCodeGen_Attribute_ThrewException_CodeTypeMember, ex.Message, prop.Name, proxyClass.Name, ex.InnerException.Message),
propertyAttributes.Where(a => a.GetType() != typeof(DataMemberAttribute)),
prop.CustomAttributes,
prop.Comments);
// Generate "if (fld == null)" test for common use below
CodeExpression isRefNullExpr =
CodeGenUtilities.MakeEqualToNull(
new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fld.Name));
// Generate a delegate style invoke of our filter method for common use below
CodeExpression filterDelegate =
CodeGenUtilities.MakeDelegateCreateExpression(this.ClientProxyGenerator.IsCSharp, new CodeTypeReference("System.Func"), filterMethod.Name);
// only generate attach and detach when the relation is two-way
PropertyDescriptor reverseAssociationMember = GetReverseAssociation(pd, associationAttribute);
bool isBiDirectionalAssociation = (reverseAssociationMember != null) && this.CanGenerateProperty(reverseAssociationMember);
if (isBiDirectionalAssociation)
{
if (IsCollectionType(pd.PropertyType))
{
CodeMemberMethod attach = this.GenerateAttach(proxyClass, elementType, associationAttribute, pd);
CodeMemberMethod detach = this.GenerateDetach(proxyClass, elementType, associationAttribute, pd);
//// generate :
//// if (_PurchaseOrderDetails == null) {
//// _PurchaseOrderDetails = new EntityCollection<PurchaseOrderDetail>(this, filter_PurchaseOrderDetails, attach_PurchaseOrderDetails, detach_PurchaseOrderDetails);
//// }
//.........这里部分代码省略.........
示例14: GenerateProperty
protected override void GenerateProperty(PropertyDescriptor propertyDescriptor)
{
base.GenerateProperty(propertyDescriptor);
AttributeCollection propertyAttributes = propertyDescriptor.ExplicitAttributes();
bool hasKeyAttr = (propertyAttributes[typeof(KeyAttribute)] != null);
if (hasKeyAttr)
{
this._keyProperties.Add(propertyDescriptor);
}
}
示例15: GenerateNonSerializableProperty
protected override bool GenerateNonSerializableProperty(PropertyDescriptor propertyDescriptor)
{
AttributeCollection propertyAttributes = propertyDescriptor.ExplicitAttributes();
AssociationAttribute associationAttr = (AssociationAttribute)propertyAttributes[typeof(AssociationAttribute)];
bool externalReference = propertyAttributes[typeof(ExternalReferenceAttribute)] != null;
if (associationAttr != null)
{
// generate Association members for members marked Association and of a Type that is exposed by the provider
this.GenEntityAssocationProperty(this.ProxyClass, propertyDescriptor, associationAttr, externalReference);
return true;
}
return false;
}