Fre*_*Boy 6 c# generics null enums
如何进行以下扩展工作?我将ComboBoxe绑定到枚举,在这种情况下,它不会编译,因为它返回null.
public static T GetSelectedValue<T>(this ComboBox control)
{
if (control.SelectedValue == null)
return null;
return (T)control.SelectedValue;
}
Run Code Online (Sandbox Code Playgroud)
注意:我希望它返回null(而不是默认值(T)).问题是,我必须使用的表达方式是什么?
返回一个可空的而不是一个普通的T:
public static T? GetSelectedValue<T>(this ComboBox control) where T : struct
{
if (control.SelectedValue == null)
return null;
return (T)control.SelectedValue;
}
Run Code Online (Sandbox Code Playgroud)