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); }
public void AddAttribute(Attribute attribute) { var attributes = _attributeCollection.Cast <Attribute>().ToList(); attributes.Add(attribute); _attributeCollection = new AttributeCollection(attributes.ToArray()); }
public void Ctor_Attributes(int count) { Attribute[] attributes = GetAttributes().Take(count).ToArray(); var attributeCollection = new AttributeCollection(attributes); Assert.Equal(attributes, attributeCollection.Cast <Attribute>()); }
/// <summary> /// Method called by the EvaluationEngine when an attribute is not found. /// </summary> /// <param name="context">The evaluation context instance.</param> /// <param name="designator">The attribute designator.</param> /// <returns>An instance of an Attribute with it's value.</returns> public AttributeElement GetAttribute(EvaluationContext context, AttributeDesignatorBase designator) { if (context == null) { throw new ArgumentNullException("context"); } if (designator == null) { throw new ArgumentNullException("designator"); } foreach (var attrib in _attributes.Cast <AttributeElement>().Where(attrib => attrib.AttributeId == designator.AttributeId)) { if (!string.IsNullOrEmpty(designator.Issuer)) { if (designator.Issuer == attrib.Issuer) { return(attrib); } } else { return(attrib); } } return(null); }
public void Ctor_Attributes(Attribute[] attributes) { var attributeCollection = new AttributeCollection(attributes); Assert.Equal(attributes?.Length ?? 0, attributeCollection.Count); Assert.Equal(attributes ?? new Attribute[0], attributeCollection.Cast <Attribute>()); }
public static bool Contains(this AttributeCollection attributes, string tag) { Require.ParameterNotNull(attributes, nameof(attributes)); return(attributes.Cast <AttributeReference>() .Any(a => a.Tag == tag)); }
public void FromExisting_NullNewAttributes_Success() { Attribute[] existingAttributes = GetAttributes().Take(5).ToArray(); var existing = new AttributeCollection(existingAttributes); AttributeCollection attributeCollection = AttributeCollection.FromExisting(existing, null); Assert.Equal(existingAttributes, attributeCollection.Cast <Attribute>()); }
public void FromExisting_DifferentNewAttributes_Success() { Attribute[] existingAttributes = GetAttributes().Take(2).ToArray(); Attribute[] newAttributes = GetAttributes().Skip(2).Take(2).ToArray(); var existing = new AttributeCollection(existingAttributes); AttributeCollection attributeCollection = AttributeCollection.FromExisting(existing, newAttributes); Assert.Equal(existingAttributes.Concat(newAttributes), attributeCollection.Cast <Attribute>()); }
public void CopyTest(int count, int index) { var attributes = GetAttributes().Take(count).ToArray(); var attributeCollection = new AttributeCollection(attributes); var array = new Attribute[count + index]; attributeCollection.CopyTo(array, index); Assert.Equal(attributeCollection.Cast<Attribute>(), array.Cast<Attribute>().Skip(index)); }
public static void SetValue(this AttributeCollection attributes, string tag, string value) { Require.ParameterNotNull(attributes, nameof(attributes)); var attribute = attributes.Cast <AttributeReference>() .FirstOrDefault(a => a.Tag == tag); Require.ObjectNotNull(attribute, $"No {nameof(AttributeReference)} with Tag '{tag}' found"); attribute.TextString = value; }
public void CopyTo_ValidArray_Success(int count, int index) { Attribute[] attributes = GetAttributes().Take(count).ToArray(); var attributeCollection = new AttributeCollection(attributes); var array = new Attribute[count + index]; attributeCollection.CopyTo(array, index); Assert.Equal(attributeCollection.Cast <Attribute>(), array.Cast <Attribute>().Skip(index)); }
public void GetAttributes_Skips_ComVisibleAttribute_And_GuidAttribute_And_InterfaceTypeAttribute() { AttributeCollection attributeCollection = TypeDescriptor.GetAttributes(typeof(TestClass1)); Assert.NotEmpty(attributeCollection); IEnumerable <Attribute> attributes = attributeCollection.Cast <Attribute>(); Attribute attribute = Assert.Single(attributes); Assert.IsType <DescriptionAttribute>(attribute); Assert.DoesNotContain(attributes, attr => attr.GetType() == typeof(ComVisibleAttribute)); Assert.DoesNotContain(attributes, attr => attr.GetType() == typeof(GuidAttribute)); Assert.DoesNotContain(attributes, attr => attr.GetType() == typeof(InterfaceTypeAttribute)); }
/// <summary> /// Returns the list of all the attributes that need to be propagated to the client /// </summary> /// <param name="typeAttributes">List of attributes on the type</param> /// <returns>List of attributes to be propagated to the client</returns> private IEnumerable <Attribute> FilterTypeAttributes(AttributeCollection typeAttributes) { List <Attribute> filteredAttributes = new List <Attribute>(); // Ignore DefaultMemberAttribute if it has been put for an indexer IEnumerable <Attribute> defaultMemberAttribs = typeAttributes.Cast <Attribute>().Where(a => a.GetType() == typeof(DefaultMemberAttribute)); if (defaultMemberAttribs.Any()) { HashSet <string> properties = new HashSet <string>(TypeDescriptor.GetProperties(this.Type).Cast <PropertyDescriptor>().Select(p => p.Name), StringComparer.Ordinal); foreach (DefaultMemberAttribute attrib in defaultMemberAttribs) { if (!properties.Contains(attrib.MemberName)) { filteredAttributes.Add(attrib); } } } // Filter out attributes in filteredAttributes as well as DataContractAttribute and KnownTypeAttribute (since they are already handled) return(typeAttributes.Cast <Attribute>().Where(a => a.GetType() != typeof(DataContractAttribute) && a.GetType() != typeof(KnownTypeAttribute) && !(filteredAttributes.Contains(a)))); }
public static void SetValue(this AttributeCollection attributes, string tag, string value) { var attribute = attributes.Cast <AttributeReference>() .FirstOrDefault(a => a.Tag == tag); if (attribute == null) { throw Error.KeyNotFound(tag); } else { attribute.TextString = value; } }
private static IEnumerable<AttributeReference> GetAttributeReferences(AttributeCollection attributes, OpenMode openMode) { Require.ParameterNotNull(attributes, nameof(attributes)); if (attributes.Count > 0) { if (attributes[0].Database.TransactionManager.TopTransaction == null) { throw new InvalidOperationException("No transaction available"); } var transaction = attributes[0].Database.TransactionManager.TopTransaction; return attributes.Cast<ObjectId>() .Select(id => (AttributeReference)transaction.GetObject(id, openMode)); } else { return Array.Empty<AttributeReference>(); } }
public void FromExisting_SameNewAttributes_Success() { Attribute[] existingAttributes = GetAttributes().Take(2).ToArray(); Attribute[] newAttributes = GetAttributes().Skip(1).Take(2).ToArray(); var existing = new AttributeCollection(existingAttributes); AttributeCollection attributeCollection = AttributeCollection.FromExisting(existing, newAttributes); Assert.Equal(new Attribute[] { existingAttributes[0], newAttributes[0], newAttributes[1] }, attributeCollection.Cast <Attribute>()); }
public static bool Contains(this AttributeCollection attributes, string tag) { return(attributes.Cast <AttributeReference>() .Any(a => a.Tag == tag)); }
/// <summary> /// Checks to see if an attribute collection contains any attributes of the provided type. /// </summary> /// <typeparam name="TAttribute">The attribute type to check for</typeparam> /// <param name="attributes">The attribute collection to inspect</param> /// <returns><c>True</c> if an attribute of the provided type is contained in the attribute collection.</returns> public static bool ContainsAttributeType <TAttribute>(this AttributeCollection attributes) where TAttribute : Attribute { return(attributes.Cast <Attribute>().Any(a => a.GetType() == typeof(TAttribute))); }
/// <summary> /// Generates the client proxy code for a domain service. /// </summary> public override void Generate() { // ---------------------------------------------------------------- // Namespace // ---------------------------------------------------------------- Type domainServiceType = this._domainServiceDescription.DomainServiceType; CodeNamespace ns = this.ClientProxyGenerator.GetOrGenNamespace(domainServiceType); AttributeCollection attributes = this._domainServiceDescription.Attributes; // Missing namespace bails out of code-gen -- error has been logged if (ns == null) { return; } // ---------------------------------------------------------------- // public partial sealed class {Name} : DomainContext // ---------------------------------------------------------------- string clientTypeName = DomainContextTypeName(this._domainServiceDescription); CodeTypeDeclaration proxyClass = CodeGenUtilities.CreateTypeDeclaration(clientTypeName, domainServiceType.Namespace); proxyClass.IsPartial = true; proxyClass.TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed; ns.Types.Add(proxyClass); CodeTypeReference domainContextTypeName = CodeGenUtilities.GetTypeReference(TypeConstants.DomainContextTypeFullName, ns.Name, false); proxyClass.BaseTypes.Add(domainContextTypeName); // Add <summary> xml comment to class string comment = string.Format(CultureInfo.CurrentCulture, Resource.CodeGen_DomainContext_Class_Summary_Comment, domainServiceType.Name); proxyClass.Comments.AddRange(CodeGenUtilities.GenerateSummaryCodeComment(comment, this.ClientProxyGenerator.IsCSharp)); // ---------------------------------------------------------------- // [DomainIdentifier], etc attributes move through metadata pipeline // ---------------------------------------------------------------- CustomAttributeGenerator.GenerateCustomAttributes( this.ClientProxyGenerator, proxyClass, ex => string.Format(CultureInfo.CurrentCulture, Resource.ClientCodeGen_Attribute_ThrewException_CodeType, ex.Message, proxyClass.Name, ex.InnerException.Message), attributes.Cast <Attribute>(), proxyClass.CustomAttributes, proxyClass.Comments); // ---------------------------------------------------------------- // Add default OnCreated partial method // ---------------------------------------------------------------- NotificationMethodGenerator notificationMethodGen = new NotificationMethodGenerator(this.ClientProxyGenerator); proxyClass.Members.AddRange(notificationMethodGen.PartialMethodsSnippetBlock); // ---------------------------------------------------------------- // Generate a contract interface for the service. // ---------------------------------------------------------------- CodeTypeDeclaration contractInterface = this.GenerateContract(proxyClass); // ---------------------------------------------------------------- // Generate constructors // ---------------------------------------------------------------- EnableClientAccessAttribute enableClientAccessAttribute = attributes.OfType <EnableClientAccessAttribute>().Single(); this.GenerateConstructors(proxyClass, contractInterface, enableClientAccessAttribute, notificationMethodGen.OnCreatedMethodInvokeExpression); // ---------------------------------------------------------------- // Separate proxies for each domain operation entry // ---------------------------------------------------------------- DomainOperationEntryProxyGenerator methodProxyGenerator = new DomainOperationEntryProxyGenerator(this.ClientProxyGenerator, proxyClass, this._domainServiceDescription); methodProxyGenerator.Generate(); // ---------------------------------------------------------------- // Invoke operations // ---------------------------------------------------------------- InvokeOperationProxyGenerator invokeOperationProxyGenerator = new InvokeOperationProxyGenerator(this.ClientProxyGenerator, proxyClass, this._domainServiceDescription); invokeOperationProxyGenerator.Generate(); // ---------------------------------------------------------------- // EntityContainer instantiation // ---------------------------------------------------------------- // The entity container holds a collection of EntityLists, one per visible entity root type. // The derived entity types are stored in their respective root's list and do not get their own. this.GenEntityContainer(proxyClass, this._domainServiceDescription.RootEntityTypes, this._domainServiceDescription); // Register created CodeTypeDeclaration with mapping this._typeMapping[domainServiceType] = proxyClass; }