internal static void EmitReflectedProperties(ref CodeTypeDeclaration customConfigCodeClass, Type typeToReflect, out PropertyNameType[] propertiesGenerated, bool excludeBaseClassProperties, string constantsClassName, string defaultValuesClassName) { // by default, include base classes BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance; // if asked to exclude, modify BindingFlags if (excludeBaseClassProperties) { bindingFlags = bindingFlags | BindingFlags.DeclaredOnly; } // Future TODO: anyway to filter out which of the public properties we want to expose in config? PropertyInfo[] allProperties = typeToReflect.GetProperties(bindingFlags); PropertyNameType[] allPropNames = new PropertyNameType[allProperties.Length]; int index = 0; foreach (PropertyInfo propertyInfo in allProperties) { // Only expose properties in config that can be written if (propertyInfo.CanWrite) { // Including types present in base classes as well PropertyNameType nameType; nameType.propertyName = propertyInfo.Name; nameType.propertyType = propertyInfo.PropertyType; customConfigCodeClass.Members.Add(CodeGenHelperMethods.CreateProperty(nameType, constantsClassName, defaultValuesClassName)); allPropNames.SetValue(nameType, index++); } } propertiesGenerated = new PropertyNameType[index]; Array.Copy(allPropNames, propertiesGenerated, index); }
static CodeMemberProperty CreateProperty(PropertyNameType nameType, string constantsClassName, string defaultValuesClassName) { CodeMemberProperty publicProp = new CodeMemberProperty(); publicProp.Name = nameType.propertyName; publicProp.Attributes = CodeDomHelperObjects.PublicFinal; publicProp.HasGet = true; publicProp.HasSet = true; publicProp.Type = new CodeTypeReference(nameType.propertyType); CodeAttributeDeclarationCollection attributes = new CodeAttributeDeclarationCollection(); CodeAttributeArgument arg1 = new CodeAttributeArgument( new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(constantsClassName), nameType.propertyName)); CodeAttributeArgument arg2 = new CodeAttributeArgument( PropertyNameConstants.DefaultValueProperty, new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(defaultValuesClassName), Constants.DefaultPrefix + nameType.propertyName)); // Future TODO: Provide a library with attributes that custom channel writes // can use to adorn their properties with default values and validators, we can // then add it here. attributes.Add(new CodeAttributeDeclaration( new CodeTypeReference(TypeNameConstants.ConfigurationProperty), arg1, arg2)); publicProp.CustomAttributes = new CodeAttributeDeclarationCollection(attributes); string nameInConfig = constantsClassName + "." + nameType.propertyName; CodeArrayIndexerExpression baseIndexedProperty = new CodeArrayIndexerExpression( CodeDomHelperObjects.BaseRef, new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(constantsClassName), nameType.propertyName)); publicProp.GetStatements.Add(new CodeMethodReturnStatement( new CodeCastExpression( nameType.propertyType, baseIndexedProperty))); publicProp.SetStatements.Add(new CodeAssignStatement( baseIndexedProperty, new CodePropertySetValueReferenceExpression())); return(publicProp); }
static CodeMemberProperty CreateProperty(PropertyNameType nameType, string constantsClassName, string defaultValuesClassName) { CodeMemberProperty publicProp = new CodeMemberProperty(); publicProp.Name = nameType.propertyName; publicProp.Attributes = CodeDomHelperObjects.PublicFinal; publicProp.HasGet = true; publicProp.HasSet = true; publicProp.Type = new CodeTypeReference(nameType.propertyType); CodeAttributeDeclarationCollection attributes = new CodeAttributeDeclarationCollection(); CodeAttributeArgument arg1 = new CodeAttributeArgument( new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(constantsClassName), nameType.propertyName)); CodeAttributeArgument arg2 = new CodeAttributeArgument( PropertyNameConstants.DefaultValueProperty, new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(defaultValuesClassName), Constants.DefaultPrefix + nameType.propertyName)); // Future TODO: Provide a library with attributes that custom channel writes // can use to adorn their properties with default values and validators, we can // then add it here. attributes.Add(new CodeAttributeDeclaration( new CodeTypeReference(TypeNameConstants.ConfigurationProperty), arg1, arg2)); publicProp.CustomAttributes = new CodeAttributeDeclarationCollection(attributes); string nameInConfig = constantsClassName + "." + nameType.propertyName; CodeArrayIndexerExpression baseIndexedProperty = new CodeArrayIndexerExpression( CodeDomHelperObjects.BaseRef, new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(constantsClassName), nameType.propertyName)); publicProp.GetStatements.Add(new CodeMethodReturnStatement( new CodeCastExpression( nameType.propertyType, baseIndexedProperty))); publicProp.SetStatements.Add(new CodeAssignStatement( baseIndexedProperty, new CodePropertySetValueReferenceExpression())); return publicProp; }
internal static CodeTypeDeclaration EmitDefaultValuesClass(PropertyNameType[] propertiesGenerated, string defaultValuesClassName) { CodeTypeDeclaration defaultValuesClass = new CodeTypeDeclaration(defaultValuesClassName); defaultValuesClass.TypeAttributes = TypeAttributes.NotPublic; MemberAttributes fieldAttrs = CodeDomHelperObjects.InternalConstants; CodeComment comment = new CodeComment("TODO: Please initialize default values to these fields"); defaultValuesClass.Comments.Add(new CodeCommentStatement(comment)); for (int i = 0; i < propertiesGenerated.Length; i++) { CodeMemberField field = new CodeMemberField( new CodeTypeReference(propertiesGenerated[i].propertyType.Name), Constants.DefaultPrefix + propertiesGenerated[i].propertyName); field.Attributes = fieldAttrs; defaultValuesClass.Members.Add(field); } return defaultValuesClass; }
internal static void EmitPropertiesProperty(ref CodeTypeDeclaration customConfigCodeClass, PropertyNameType[] propertiesGenerated, string constantsClassName, string defaultValuesClassName) { CodeTypeReference configPropCollectionType = new CodeTypeReference( TypeNameConstants.ConfigurationPropertyCollection); CodeMemberProperty propertiesProperty = new CodeMemberProperty(); propertiesProperty.Name = PropertyNameConstants.PropertiesProperty; propertiesProperty.Attributes = CodeDomHelperObjects.ProtectedOverride; propertiesProperty.HasSet = false; propertiesProperty.HasGet = true; propertiesProperty.Type = configPropCollectionType; string varProperties = Helpers.TurnFirstCharLower(PropertyNameConstants.PropertiesProperty); CodeVariableDeclarationStatement cvds = new CodeVariableDeclarationStatement( configPropCollectionType, varProperties, new CodePropertyReferenceExpression( CodeDomHelperObjects.BaseRef, PropertyNameConstants.PropertiesProperty)); propertiesProperty.GetStatements.Add(cvds); CodeVariableReferenceExpression varPropsRef = new CodeVariableReferenceExpression(varProperties); foreach (PropertyNameType nameType in propertiesGenerated) { CodeObjectCreateExpression paramToAddMethod = new CodeObjectCreateExpression( TypeNameConstants.ConfigurationProperty, new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(constantsClassName), nameType.propertyName), new CodeTypeOfExpression( new CodeTypeReference(nameType.propertyType)), new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(defaultValuesClassName), Constants.DefaultPrefix + nameType.propertyName)); CodeMethodInvokeExpression cmie = new CodeMethodInvokeExpression( varPropsRef, MethodNameConstants.AddMethod, paramToAddMethod); propertiesProperty.GetStatements.Add(cmie); } propertiesProperty.GetStatements.Add(new CodeMethodReturnStatement(varPropsRef)); customConfigCodeClass.Members.Add(propertiesProperty); }
internal static void EmitReflectedProperties(ref CodeTypeDeclaration customConfigCodeClass, Type typeToReflect, out PropertyNameType[] propertiesGenerated, string constantsClassName, string defaultValuesClassName) { EmitReflectedProperties(ref customConfigCodeClass, typeToReflect, out propertiesGenerated, false, constantsClassName, defaultValuesClassName); }
internal static CodeTypeDeclaration EmitConstantStringsClass(PropertyNameType[] propertiesGenerated, string constantsClassName) { CodeTypeDeclaration constantsClass = new CodeTypeDeclaration(constantsClassName); constantsClass.TypeAttributes = TypeAttributes.NotPublic; MemberAttributes fieldAttrs = CodeDomHelperObjects.InternalConstants; for (int i = 0; i < propertiesGenerated.Length; i++) { CodeMemberField field = new CodeMemberField( CodeDomHelperObjects.stringTypeRef, propertiesGenerated[i].propertyName); field.Attributes = fieldAttrs; field.InitExpression = new CodePrimitiveExpression( Helpers.TurnFirstCharLower(propertiesGenerated[i].propertyName)); constantsClass.Members.Add(field); } return constantsClass; }