Search code examples
c#azureservicebus

How to put generic Type (T) as parameter?


I have this error:

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?) 

on the method signature for this method:

 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));
        }

Solution

  • You forgot to specify the type parameter. You can do that in 2 ways:

    Either you define them at the method definition (which is the way to go in your case, because your method is static):

    public static void SendMessage<T>(string queuName, T objeto)

    Or you can specify them on the class definition (for instance methods):

    class MyClass<T>{
        public void SendMessage(string queuName, T objeto){}
    }