private void CodeDomProducer_CodeDomProduction(object sender, CodeDomProductionEventArgs e) { if (TargetVersion != AspNetIdentityVersion.Version1 && TargetVersion != AspNetIdentityVersion.Version2) { return; } if (e.EventType == CodeDomProductionEventType.EntityCommitting) { CodeCompileUnit unit = e.Argument as CodeCompileUnit; if (unit == null) { return; } foreach (CodeNamespace ns in unit.Namespaces) { foreach (CodeTypeDeclaration typeDeclaration in ns.Types) { BaseType type = UserData.GetBaseType(typeDeclaration); if (type.GetAttributeValue("entityType", NamespaceUri, EntityType.None) == EntityType.User) { // Implements IUser<TKey> & IUser if (TargetVersion == AspNetIdentityVersion.Version1) { ImplementIUser(typeDeclaration, false, false); } else if (TargetVersion == AspNetIdentityVersion.Version2) { if (!_identityUser.IsStringId) { ImplementIUser(typeDeclaration, true, true); } ImplementIUser(typeDeclaration, false, true); } } else if (type.GetAttributeValue("entityType", NamespaceUri, EntityType.None) == EntityType.Role) { // Implements IRole<TKey> & IRole if (TargetVersion == AspNetIdentityVersion.Version1) { ImplementIRole(typeDeclaration, false, false); } else if (TargetVersion == AspNetIdentityVersion.Version2) { if (!_identityRole.IsStringId) { ImplementIRole(typeDeclaration, true, true); } ImplementIRole(typeDeclaration, false, true); } } } } } }
private void CodeDomProducer_CodeDomProduction(object sender, CodeDomProductionEventArgs e) { if (TargetVersion != AspNetIdentityVersion.Version1 && TargetVersion != AspNetIdentityVersion.Version2) return; if (e.EventType == CodeDomProductionEventType.EntityCommitting) { CodeCompileUnit unit = e.Argument as CodeCompileUnit; if (unit == null) return; foreach (CodeNamespace ns in unit.Namespaces) { foreach (CodeTypeDeclaration typeDeclaration in ns.Types) { BaseType type = UserData.GetBaseType(typeDeclaration); if (type.GetAttributeValue("entityType", NamespaceUri, EntityType.None) == EntityType.User) { // Implements IUser<TKey> & IUser if (TargetVersion == AspNetIdentityVersion.Version1) { ImplementIUser(typeDeclaration, false, false); } else if (TargetVersion == AspNetIdentityVersion.Version2) { if (!_identityUser.IsStringId) { ImplementIUser(typeDeclaration, true, true); } ImplementIUser(typeDeclaration, false, true); } } else if (type.GetAttributeValue("entityType", NamespaceUri, EntityType.None) == EntityType.Role) { // Implements IRole<TKey> & IRole if (TargetVersion == AspNetIdentityVersion.Version1) { ImplementIRole(typeDeclaration, false, false); } else if (TargetVersion == AspNetIdentityVersion.Version2) { if (!_identityRole.IsStringId) { ImplementIRole(typeDeclaration, true, true); } ImplementIRole(typeDeclaration, false, true); } } } } } }
private void CodeDomProducer_CodeDomProduction(object sender, CodeDomProductionEventArgs e) { if (e.EventType == CodeDomProductionEventType.UnitsProducing) { if (e.Argument == null) { return; } foreach (var entity in Project.Entities) { if (!entity.GetAttributeValue("enabled", NamespaceUri, true)) { continue; } CodeTypeDeclaration typeDeclaration = _codeDomProducer.GetType(entity); if (typeDeclaration == null) { continue; } // Class var jsonObjectAttribute = new CodeAttributeDeclaration("Newtonsoft.Json.JsonObjectAttribute"); string optMode = entity.GetAttributeValue("optMode", NamespaceUri, "OptIn"); if (optMode != null) { var memberSerialization = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("Newtonsoft.Json.MemberSerialization"), optMode); jsonObjectAttribute.Arguments.Add(new CodeAttributeArgument("MemberSerialization", memberSerialization)); } CodeDomUtilities.EnsureAttribute(typeDeclaration, jsonObjectAttribute, true); // Properties foreach (CodeTypeMember member in typeDeclaration.Members) { PropertyDefinition propertyDefinition = null; CodeMemberProperty memberProperty = member as CodeMemberProperty; if (memberProperty != null) { propertyDefinition = UserData.GetPropertyDefinition(memberProperty); } Property property = null; if (propertyDefinition != null && propertyDefinition.Property != null) { property = propertyDefinition.Property; if (!property.GetAttributeValue("enabled", NamespaceUri, true)) { continue; } } else { if (!entity.GetAttributeValue("enabledForNonModelProperties", NamespaceUri, true)) { continue; } } bool?serialized = null; if (property != null) { serialized = property.GetAttributeValue("includeInSerialization", NamespaceUri, (bool?)null); } if (serialized == null) { //[System.NonSerializedAttribute()] => false //[System.Xml.Serialization.XmlIgnoreAttribute()] => false //[System.Runtime.Serialization.DataMemberAttribute()] => true if (CodeDomUtilities.GetAttribute(member, typeof(NonSerializedAttribute)) != null) { serialized = false; } else if (CodeDomUtilities.GetAttribute(member, typeof(XmlIgnoreAttribute)) != null) { serialized = false; } else if (CodeDomUtilities.GetAttribute(member, typeof(DataMemberAttribute)) != null) { serialized = true; } else if (CodeDomUtilities.GetAttribute(member, typeof(XmlAttribute)) != null) { serialized = true; } else if (CodeDomUtilities.GetAttribute(member, typeof(XmlElementAttribute)) != null) { serialized = true; } else if (property != null) { serialized = property.IsIncludedInSerialization; } } // [JsonIgnore] or [JsonProperty] if (serialized != null) { var jsonPropertyAttribute = new CodeAttributeDeclaration(); jsonPropertyAttribute.Name = serialized == true ? "Newtonsoft.Json.JsonPropertyAttribute" : "Newtonsoft.Json.JsonIgnoreAttribute"; CodeDomUtilities.EnsureAttribute(member, jsonPropertyAttribute, true); } } } } }
private void CodeDomProducer_CodeDomProduction(object sender, CodeDomProductionEventArgs e) { if (e.EventType == CodeDomProductionEventType.UnitsProducing) { if (e.Argument == null) return; foreach (var entity in Project.Entities) { if (!entity.GetAttributeValue("enabled", NamespaceUri, true)) continue; CodeTypeDeclaration typeDeclaration = _codeDomProducer.GetType(entity); if (typeDeclaration == null) continue; // Class var jsonObjectAttribute = new CodeAttributeDeclaration("Newtonsoft.Json.JsonObjectAttribute"); string optMode = entity.GetAttributeValue("optMode", NamespaceUri, "OptIn"); if (optMode != null) { var memberSerialization = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("Newtonsoft.Json.MemberSerialization"), optMode); jsonObjectAttribute.Arguments.Add(new CodeAttributeArgument("MemberSerialization", memberSerialization)); } CodeDomUtilities.EnsureAttribute(typeDeclaration, jsonObjectAttribute, true); // Properties foreach (CodeTypeMember member in typeDeclaration.Members) { PropertyDefinition propertyDefinition = null; CodeMemberProperty memberProperty = member as CodeMemberProperty; if (memberProperty != null) { propertyDefinition = UserData.GetPropertyDefinition(memberProperty); } Property property = null; if (propertyDefinition != null && propertyDefinition.Property != null) { property = propertyDefinition.Property; if (!property.GetAttributeValue("enabled", NamespaceUri, true)) continue; } else { if (!entity.GetAttributeValue("enabledForNonModelProperties", NamespaceUri, true)) continue; } bool? serialized = null; if (property != null) { serialized = property.GetAttributeValue("includeInSerialization", NamespaceUri, (bool?)null); } if (serialized == null) { //[System.NonSerializedAttribute()] => false //[System.Xml.Serialization.XmlIgnoreAttribute()] => false //[System.Runtime.Serialization.DataMemberAttribute()] => true if (CodeDomUtilities.GetAttribute(member, typeof(NonSerializedAttribute)) != null) { serialized = false; } else if (CodeDomUtilities.GetAttribute(member, typeof(XmlIgnoreAttribute)) != null) { serialized = false; } else if (CodeDomUtilities.GetAttribute(member, typeof(DataMemberAttribute)) != null) { serialized = true; } else if (CodeDomUtilities.GetAttribute(member, typeof(XmlAttribute)) != null) { serialized = true; } else if (CodeDomUtilities.GetAttribute(member, typeof(XmlElementAttribute)) != null) { serialized = true; } else if (property != null) { serialized = property.IsIncludedInSerialization; } } // [JsonIgnore] or [JsonProperty] if (serialized != null) { var jsonPropertyAttribute = new CodeAttributeDeclaration(); jsonPropertyAttribute.Name = serialized == true ? "Newtonsoft.Json.JsonPropertyAttribute" : "Newtonsoft.Json.JsonIgnoreAttribute"; CodeDomUtilities.EnsureAttribute(member, jsonPropertyAttribute, true); } } } } }
private void CodeDomProducer_CodeDomProduction(object sender, CodeDomProductionEventArgs e) { if (e.EventType == CodeDomProductionEventType.EntityCommitting) { CodeCompileUnit unit = e.Argument as CodeCompileUnit; if (unit == null) return; foreach (CodeNamespace ns in unit.Namespaces) { foreach (CodeTypeDeclaration typeDeclaration in ns.Types) { BaseType type = UserData.GetBaseType(typeDeclaration); if (type.GetAttributeValue("entityType", NamespaceUri, EntityType.None) == EntityType.User) { // Implements IUser<TKey> & IUser if (_identityUser.MustImplementGenericInterface) { ImplementIUser(typeDeclaration, true); } ImplementIUser(typeDeclaration, false); } else if (type.GetAttributeValue("entityType", NamespaceUri, EntityType.None) == EntityType.Role) { // Implements IRole<TKey> & IRole if (_identityRole.MustImplementGenericInterface) { ImplementIRole(typeDeclaration, true); } ImplementIRole(typeDeclaration, false); } //if (type.GetAttributeValue("entityType", NamespaceUri, EntityType.None) == EntityType.Claim) //{ // // Add FromClaim, ToClaim method (implicit conversion), IEquatable<Claim> // var fromClaimMethod = new CodeMemberMethod(); // fromClaimMethod.Name = "FromClaim"; // fromClaimMethod.Attributes = MemberAttributes.Public | MemberAttributes.Static; // var claimParameter = new CodeParameterDeclarationExpression(typeof(System.Security.Claims.Claim), "claim"); // fromClaimMethod.Parameters.Add(claimParameter); // fromClaimMethod.ReturnType = CodeDomUtilities.ReferenceFromDeclaration(typeDeclaration); // fromClaimMethod.Statements.Add(CodeDomUtilities.CreateThrowIfNull(new CodeArgumentReferenceExpression("claim"), "claim")); // fromClaimMethod.Statements.Add(new CodeVariableDeclarationStatement(CodeDomUtilities.ReferenceFromDeclaration(typeDeclaration), "result", new CodeObjectCreateExpression(CodeDomUtilities.ReferenceFromDeclaration(typeDeclaration)))); // if (_identityClaim.TypeProperty != null) // { // fromClaimMethod.Statements.Add(new CodeAssignStatement( // new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("result"), _identityClaim.TypeProperty.Name), // new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("claim"), "Type"))); // } // if (_identityClaim.ValueProperty != null) // { // fromClaimMethod.Statements.Add(new CodeAssignStatement( // new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("result"), _identityClaim.ValueProperty.Name), // new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("claim"), "Value"))); // } // if (_identityClaim.ValueTypeProperty != null) // { // fromClaimMethod.Statements.Add(new CodeAssignStatement( // new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("result"), _identityClaim.ValueTypeProperty.Name), // new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("claim"), "ValueType"))); // } // if (_identityClaim.IssuerProperty != null) // { // fromClaimMethod.Statements.Add(new CodeAssignStatement( // new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("result"), _identityClaim.IssuerProperty.Name), // new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("claim"), "Issuer"))); // } // if (_identityClaim.OriginalIssuerProperty != null) // { // fromClaimMethod.Statements.Add(new CodeAssignStatement( // new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("result"), _identityClaim.OriginalIssuerProperty.Name), // new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("claim"), "OriginalIssuer"))); // } // typeDeclaration.Members.Add(fromClaimMethod); // fromClaimMethod.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("result"))); //} } } } }
private void CodeDomProducer_CodeDomProduction(object sender, CodeDomProductionEventArgs e) { if (e.EventType == CodeDomProductionEventType.EntityCommitting) { CodeCompileUnit unit = e.Argument as CodeCompileUnit; if (unit == null) { return; } foreach (CodeNamespace ns in unit.Namespaces) { foreach (CodeTypeDeclaration typeDeclaration in ns.Types) { BaseType type = UserData.GetBaseType(typeDeclaration); if (type.GetAttributeValue("entityType", NamespaceUri, EntityType.None) == EntityType.User) { // Implements IUser<TKey> & IUser if (_identityUser.MustImplementGenericInterface) { ImplementIUser(typeDeclaration, true); } ImplementIUser(typeDeclaration, false); } else if (type.GetAttributeValue("entityType", NamespaceUri, EntityType.None) == EntityType.Role) { // Implements IRole<TKey> & IRole if (_identityRole.MustImplementGenericInterface) { ImplementIRole(typeDeclaration, true); } ImplementIRole(typeDeclaration, false); } //if (type.GetAttributeValue("entityType", NamespaceUri, EntityType.None) == EntityType.Claim) //{ // // Add FromClaim, ToClaim method (implicit conversion), IEquatable<Claim> // var fromClaimMethod = new CodeMemberMethod(); // fromClaimMethod.Name = "FromClaim"; // fromClaimMethod.Attributes = MemberAttributes.Public | MemberAttributes.Static; // var claimParameter = new CodeParameterDeclarationExpression(typeof(System.Security.Claims.Claim), "claim"); // fromClaimMethod.Parameters.Add(claimParameter); // fromClaimMethod.ReturnType = CodeDomUtilities.ReferenceFromDeclaration(typeDeclaration); // fromClaimMethod.Statements.Add(CodeDomUtilities.CreateThrowIfNull(new CodeArgumentReferenceExpression("claim"), "claim")); // fromClaimMethod.Statements.Add(new CodeVariableDeclarationStatement(CodeDomUtilities.ReferenceFromDeclaration(typeDeclaration), "result", new CodeObjectCreateExpression(CodeDomUtilities.ReferenceFromDeclaration(typeDeclaration)))); // if (_identityClaim.TypeProperty != null) // { // fromClaimMethod.Statements.Add(new CodeAssignStatement( // new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("result"), _identityClaim.TypeProperty.Name), // new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("claim"), "Type"))); // } // if (_identityClaim.ValueProperty != null) // { // fromClaimMethod.Statements.Add(new CodeAssignStatement( // new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("result"), _identityClaim.ValueProperty.Name), // new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("claim"), "Value"))); // } // if (_identityClaim.ValueTypeProperty != null) // { // fromClaimMethod.Statements.Add(new CodeAssignStatement( // new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("result"), _identityClaim.ValueTypeProperty.Name), // new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("claim"), "ValueType"))); // } // if (_identityClaim.IssuerProperty != null) // { // fromClaimMethod.Statements.Add(new CodeAssignStatement( // new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("result"), _identityClaim.IssuerProperty.Name), // new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("claim"), "Issuer"))); // } // if (_identityClaim.OriginalIssuerProperty != null) // { // fromClaimMethod.Statements.Add(new CodeAssignStatement( // new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("result"), _identityClaim.OriginalIssuerProperty.Name), // new CodePropertyReferenceExpression(new CodeArgumentReferenceExpression("claim"), "OriginalIssuer"))); // } // typeDeclaration.Members.Add(fromClaimMethod); // fromClaimMethod.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("result"))); //} } } } }