我怎样才能保证T是一堂课?(How can I guarantee that T is a class?)

我有下一个代码:

public static T GetSerializedCopy<T> (T src) { //some code to obtain a copy }

我怎样才能保证T将会是一个类,而不是结构体,枚举或其他东西?

I have the next code:

public static T GetSerializedCopy<T> (T src) { //some code to obtain a copy }

How I can guarantee that T will be a class, not a struct or enum or simething else?

最满意答案

这很容易确保...

public static T GetSerializedCopy<T>(T src) where T : class { //some code to obtain a copy return default(T); }

It's pretty easy to ensure that...

public static T GetSerializedCopy<T>(T src) where T : class { //some code to obtain a copy return default(T); }我怎样才能保证T是一堂课?(How can I guarantee that T is a class?)

我有下一个代码:

public static T GetSerializedCopy<T> (T src) { //some code to obtain a copy }

我怎样才能保证T将会是一个类,而不是结构体,枚举或其他东西?

I have the next code:

public static T GetSerializedCopy<T> (T src) { //some code to obtain a copy }

How I can guarantee that T will be a class, not a struct or enum or simething else?

最满意答案

这很容易确保...

public static T GetSerializedCopy<T>(T src) where T : class { //some code to obtain a copy return default(T); }

It's pretty easy to ensure that...

public static T GetSerializedCopy<T>(T src) where T : class { //some code to obtain a copy return default(T); }