我有这个错误:
Severity Code Description Project File Line
Error CS0246 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)
关于此方法的方法签名:
public static void SendMessage(string queuName, T objeto)
{
QueueClient Client =QueueClient.CreateFromConnectionString(connectionString, "Empresa");
BrokeredMessage message = new BrokeredMessage(objeto);
message.ContentType = objeto.GetType().Name;
Client.Send(new BrokeredMessage(message));
}
答案 0 :(得分:5)
public static void SendMessage<T>(string queuName, T objeto)
{
QueueClient Client =QueueClient.CreateFromConnectionString(connectionString, "Empresa");
BrokeredMessage message = new BrokeredMessage(objeto);
message.ContentType = objeto.GetType().Name;
Client.Send(new BrokeredMessage(message));
}
答案 1 :(得分:3)
您忘记指定类型参数。 你可以用两种方式做到这一点:
您可以在方法定义中定义它们(这是您的方法,因为您的方法是静态的):
public static void SendMessage<T>(string queuName, T objeto)
或者您可以在类定义上指定它们(例如方法):
class MyClass<T>{
public void SendMessage(string queuName, T objeto){}
}
答案 2 :(得分:1)
您的示例的正确语法是:
public static void SendMessage<T>(string queuName, T objeto)
{
// Type of T is
Type t = typeof(T);
// Obtain Name
string name = t.Name
// Create another instance of T
object to = Activator.CreateInstance<T>();
// etc.
}
一般来说:
T method<T>(T param) where T: restrictions //new() for example
{ return (T)Activator.CreateInstance<T>(); }