Exemplo n.º 1
0
        /// <summary>
        /// Method creates CodeCompileUnit that represents enumeration
        /// NOTE: in this method if character '-' is found it is replaced by string "MINUS"
        /// so that the member field names in CodeCompileUnit ca be valid for C# code
        /// e.g. Hz-1 will become HzMINUS1
        /// </summary>
        /// <param name="package">ProfileElement element that represents the package</param>
        /// <param name="entity">ProfileElement element that represents enumeration</param>
        /// <returns>CodeCompileUnit unit representing enumeration</returns>
        private CodeCompileUnit CreateEnumeration(ProfileElement package, Class entity)
        {
            CodeCompileUnit unit = new CodeCompileUnit();
            //namespace
            string fullNS = package.Name;

            ProfileElement temp = package;

            /*while(temp.BelongsToCategoryAsObject != null)
             * {
             *  temp = temp.BelongsToCategoryAsObject;
             *  fullNS = temp.Name + "." + fullNS;
             * }
             * fullNS = defaultNS + "." + fullNS;*/
            //CodeNamespace nameSpace = new CodeNamespace(fullNS);
            CodeNamespace nameSpace = new CodeNamespace(defaultNS);

            unit.Namespaces.Add(nameSpace);

            //namespace imports
            nameSpace.Imports.Add(new CodeNamespaceImport("System"));

            CodeTypeDeclaration enumeration = new CodeTypeDeclaration(entity.Name);

            enumeration.IsEnum         = true;
            enumeration.Attributes     = MemberAttributes.Public;
            enumeration.TypeAttributes = TypeAttributes.Public;

            if (entity.MyEnumerationMembers != null)
            {
                foreach (ProfileElement enumMember in entity.MyEnumerationMembers)
                {
                    /*
                     * REPLACES '-' with "MINUS" because in enumeration '-' is not a valid character
                     */
                    string value = enumMember.Name;
                    value = StringManipulationManager.ReplaceInvalidEnumerationCharacters(value);
                    CodeMemberField val = new CodeMemberField(typeof(int), value);
                    if (!string.IsNullOrEmpty(enumMember.Comment))
                    {
                        val.Comments.Add(new CodeCommentStatement(enumMember.Comment, true));
                    }
                    enumeration.Members.Add(val);
                }
            }
            nameSpace.Types.Add(enumeration);
            return(unit);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Method creates CodeCompileUnit that represents enumeration
        /// NOTE: in this method if character '-' is found it is replaced by string "MINUS"
        /// so that the member field names in CodeCompileUnit ca be valid for C# code
        /// e.g. Hz-1 will become HzMINUS1
        /// </summary>
        /// <param name="package">ProfileElement element that represents the package</param>
        /// <param name="entity">ProfileElement element that represents enumeration</param>
        /// <returns>CodeCompileUnit unit representing enumeration</returns>
        private CodeCompileUnit CreateEnumeration(ProfileElement package, Class entity)
        {
            CodeCompileUnit unit = new CodeCompileUnit();
            //namespace
            string fullNS = package.Name;

            ProfileElement temp = package;

            //CodeNamespace nameSpace = new CodeNamespace(fullNS);
            CodeNamespace nameSpace = new CodeNamespace(defaultNS);

            unit.Namespaces.Add(nameSpace);

            //namespace imports
            nameSpace.Imports.Add(new CodeNamespaceImport("System"));

            CodeTypeDeclaration enumeration = new CodeTypeDeclaration(entity.Name);

            enumeration.IsEnum         = true;
            enumeration.Attributes     = MemberAttributes.Public;
            enumeration.TypeAttributes = TypeAttributes.Public;

            if (entity.MyEnumerationMembers != null)
            {
                foreach (ProfileElement enumMember in entity.MyEnumerationMembers)
                {
                    string value = enumMember.Name;
                    value = StringManipulationManager.ReplaceInvalidEnumerationCharacters(value);
                    CodeMemberField val = new CodeMemberField(typeof(int), value);
                    if (!string.IsNullOrEmpty(enumMember.Comment))
                    {
                        val.Comments.Add(new CodeCommentStatement(enumMember.Comment, true));
                    }
                    enumeration.Members.Add(val);
                }
            }
            nameSpace.Types.Add(enumeration);
            return(unit);
        }
 /// <summary>
 /// Sets value of enumeration to property <c>prop</c> of object <c>instance</c>
 /// </summary>
 /// <param name="assembly">contains enumeration definition</param>
 /// <param name="instance">object that contains property</param>
 /// <param name="prop">property that value will be set to</param>
 /// <param name="att">attribute from model that cointains value and name of enumeration</param>
 private void SetEnumerationProperty(CIMObject element, Assembly assembly, object instance, PropertyInfo prop, ObjectAttribute att)
 {
     if (!string.IsNullOrEmpty(att.Value))
     {
         ////get type, that is, enumeration member that has name equal to the string value of attribute
         ////get the field with name casted into int and set itatt.Value
         Type      enumType = assembly.GetType(prop.PropertyType.ToString());
         FieldInfo enumVal  = enumType.GetField(StringManipulationManager.ReplaceInvalidEnumerationCharacters(att.Value));
         if (enumVal == null)
         {
             OnMessage("Error occured while trying to set value to field " +
                       prop.DeclaringType.Name + "." +
                       prop.Name + ", enumeration value <" +
                       prop.PropertyType.ToString() + "." + StringManipulationManager.ReplaceInvalidEnumerationCharacters(att.Value) + "> not found in assembly!"
                       + " elements ID: " + element.ID, MessageLevel.WARNING);
         }
         else
         {
             object newEnumValue = enumVal.GetValue(enumType);
             prop.SetValue(instance, newEnumValue, null);
         }
     }
 }