开发者

How to assign multiple enum values to a property in Spring.NET configuration?

.NET supports the FlagsAttribute for enum's, which indicates "that an enumeration can be treated as a bit field".

An example of an enum that makes use of this [Flags] attribute is System.Windows.Forms.Keys

Here is my Spring.NET object definition:

<object id="command.paste" type="MyNamespace.PasteClipboardCommand, MyProject">
  <property name="Title" value="Paste into Folder" />
  <property name="ShortcutKeys" value="P" />
</object>

This sets the property ShortcutKeys of type Keys to the key P. No problem there. However, since this is a shortcut, I'd like to assign something like Control+P as shortcut key.

If I would wire this up in code, it would be:

command.ShortcutKeys = Keys.Control | Keys.P;

So, maybe I was a little naive trying this:

<property name="ShortcutKeys" value="Control|P" />

This fails, because the EnumConverter can only parse/convert string representations of single enum's. A full stacktrace is available below.

  • Is this possible by default Spring.NET ?
  • Can I work around this by specifying/using another EnumConverter ?
  • Should I create/use another Keys class to do the Spring.NET initialization ?

UPDATE:

Apparently the KeysConverter knows how to handle this like this:

(new KeysConverter()).ConvertFrom(null, null, "Control+P");

Unfortunately, the EnumConverter does not support this ?

Attached Stacktrace:

System.Configuration.ConfigurationErrorsException: Error creating context 'spring.root': PropertyAccessExceptionsException (1 errors); nested PropertyAccessExceptions are: 
[Spring.Core.TypeMismatchException: Cannot convert property value of type [System.String] to required type [System.Windows.Forms.Keys] for property 'ShortcutKeys'., Inner Exception: System.FormatException: Control|P is not a valid value for Keys. ---> System.ArgumentException: Requested value 'Control|P' was not found.
    at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
    at System.ComponentModel.EnumConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
    --- End of inner exception stack trace ---
    at System.ComponentModel.EnumConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
    at System.ComponentModel.TypeConverter.ConvertFromInvariantString(String text)
    at Spring.Core.TypeConversion.TypeConversionUtils.ConvertValueIfNecessary(Type requiredType, Object newValue, String propertyName)] ---> Spring.Objects.Factory.ObjectCreationException: Error creating object with name 'command.builder.paste' defined in 'file [....\commands.xml] line 39' : Error setting property values: PropertyAccessExceptionsException (1 errors); nested PropertyAccessExceptions are: 
[Spring.Core.TypeMismatchException: Cannot convert property value of type [System.String] to required type [System.Windows.Forms.Keys] for property 'ShortcutKeys'., Inner Exception: System.FormatException: Control|P is not a valid value for Keys. ---> System.ArgumentException: Requested value 'Control|P' was 开发者_开发百科not found.
    at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
    at System.ComponentModel.EnumConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
    --- End of inner exception stack trace ---
    at System.ComponentModel.EnumConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
    at System.ComponentModel.TypeConverter.ConvertFromInvariantString(String text)
    at Spring.Core.TypeConversion.TypeConversionUtils.ConvertValueIfNecessary(Type requiredType, Object newValue, String propertyName)] ---> PropertyAccessExceptionsException (1 errors); nested PropertyAccessExceptions are: 
[Spring.Core.TypeMismatchException: Cannot convert property value of type [System.String] to required type [System.Windows.Forms.Keys] for property 'ShortcutKeys'., Inner Exception: System.FormatException: Control|P is not a valid value for Keys. ---> System.ArgumentException: Requested value 'Control|P' was not found.
    at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
    at System.ComponentModel.EnumConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
    --- End of inner exception stack trace ---
    at System.ComponentModel.EnumConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
    at System.ComponentModel.TypeConverter.ConvertFromInvariantString(String text)
    at Spring.Core.TypeConversion.TypeConversionUtils.ConvertValueIfNecessary(Type requiredType, Object newValue, String propertyName)]


If you look at the Spring.NET documentation 5.11 Configuration of IApplicationContext, you can see an example of how to add a specific type converter for a given type.

Adding the KeysConverter for type Keys in app.config is sufficient:

<converter for="System.Windows.Forms.Keys, System.Windows.Forms" type="System.Windows.Forms.KeysConverter, System.Windows.Forms"/>

Now the enumvalue/shortcut can be specified like this:

<property name="ShortcutKeys" value="Control+P" />


Try using SpEL (Spring.NET Expression Language) : http://www.springframework.net/doc-latest/reference/html/expressions.html

<property name="ShortcutKeys" expression="Keys.Control or Keys.P" />

or

<property name="ShortcutKeys" expression="T(System.Windows.Forms.Keys).Control or T(System.Windows.Forms.Keys).P" />


<property name="ShortcutKeys" value="Control, P" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜