Ambiguous Generic restriction T:class vs T:struct

后端 未结 1 1106
难免孤独
难免孤独 2021-01-15 14:25

This code generates a compiler error that the member is already defined with the same parameter types.

   private T GetProperty(Func

        
1条回答
  •  说谎
    说谎 (楼主)
    2021-01-15 14:55

    Generic type constraints can't be used for overload resolution, but you really don't need an overloaded method for this. Just use default instead of null:

    private T GetProperty(Func GetFunc)
    {
        try
        {
            return GetFunc(Properties.Settings.Default);
        }
        catch (Exception exception)
        {
            SettingReadException(this,exception);
            return default(T);
        }
    }
    

    Oh, and I've copied your code verbatim, but please don't swallow the general Exception like this. Catch the specific exception that you actually expect might be thrown. You don't want this code inadvertently swallowing OutOfMemoryException or BadImageFormatException instances.

    0 讨论(0)
提交回复
热议问题