/// <summary>
 /// Gets called when a new value is needed by the model.
 /// </summary>
 /// <param name="sender">The issuer of the event.</param>
 /// <param name="e">More information about the event.</param>
 private void expectedObjectTree_NewValueNeeded(object sender, NewValueEventArgs e)
 {
     if (e.ObjectField != null && e.ObjectField.Parent is ObjectFieldClass && e.ObjectField.Parent.Value is NRAttributeValue)
     {
         NRAttributeValue attribute = e.ObjectField.Parent.Value as NRAttributeValue;
         Type             type      = Type.GetType(attribute.Type);
         if (type != null)
         {
             e.Value = Activator.CreateInstance(type);
         }
     }
 }
示例#2
0
文件: CSharp.cs 项目: Victov/NClass
        /// <summary>
        ///     Gets the C#-Code representing the value of the attribute.
        /// </summary>
        /// <param name="value">The attribute value to get the code for.</param>
        /// <returns>The C#-Code for the value.</returns>
        private static string GetAttributeValueString(NRAttributeValue value)
        {
            if (value.Type == "System.String")
            {
                return("\"" + value.Value + "\"");
            }
            if (value.Type == "System.Type")
            {
                return("typeof(" + value.Value + ")");
            }
            Type type = Type.GetType(value.Type, false);

            if ((type != null) && type.IsEnum)
            {
                try
                {
                    string        format = Enum.Format(type, value.Value, "F");
                    StringBuilder result = new StringBuilder( );
                    foreach (string constant in format.Split(new[] { ", " }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        result.Append(type.FullName + "." + constant + " || ");
                    }
                    if (result.Length > 0)
                    {
                        result.Length -= 4;
                    }

                    return(result.ToString( ));
                }
                catch (Exception)
                {
                    return(value.Value.ToString( ));
                }
            }
            return(value.Value.ToString( ));
        }