c# - 默认情况下将泛型类型参数设置为 object c#

标签 c# .net-4.0

给出一个方法:

private static T GetBin<T>(string file)

如果我使用的话,是否可以将 T 设置为默认为 object:

public static byte[] ToJSONBytes<T>(this T obj) 

它默认为 obj 设置的内容。但是,此 GetBin 方法使用 BinaryFormatter 反序列化文件,并且众所周知,它返回一个对象。我的方法将其显式转换为 T 然后返回它,但我想让 T 可选并默认为 object

最佳答案

泛型类型参数不能有默认值。

但是,它们可能会过载:

private static object GetBin(string file) { return GetBin<object>(file); }

关于c# - 默认情况下将泛型类型参数设置为 object c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15367201/

相关文章:

ASP.NET 处理程序未在 IIS7 上运行

c# - 在自定义条件 ValidationAttribute 中检索条件值

c# - 为什么我在使用 SetPixel : SetPixel is not supported for images with indexed pixel formats? 时出现异常

c# - String.Replace() 与 StringBuilder.Replace()

c# - C#如何进行动态绑定(bind)?

c# - 在 ASP.NET aspx 页面中使用 `<%= "{0}, {1 }", arg1, arg2 %>` 代替 `<%= string.Format("{0}, {1 }", arg1, arg2) %>` 是否有效

c# - 我需要引用哪些包来创建我自己的 NUnit 控制台运行程序?

c# - Web API、IQueryable、OData 和 HttpResponseMessage 作为返回类型?

c# - 如何让 C# Cmdlet 参数 HelpMessage 显示在 `Get-Help` 中

具有属性的类实例化的 C# 到 VB.NET 语法转换