Allan Jiang
Allan Jiang

Reputation: 11331

C# Generic type event args

Here is my class definition for a customized event args

using System;

public class DeserializeEventArgs<T> : EventArgs
{
    public DeserializeEventArgs(T deserializeResult)
    {
        this.DeserializeResult = deserializeResult;
    }

    public T DeserializeResult
    {
        get;
        private set;
    }
}

And I want to do this in the code where I want to fire this event

public event EventHandler<DeserializeEventArgs<T>> DeserializeEvent;

And it will not compile (red line under T says no type found). Not sure if this is the right way of using it, anyone has experience please share some idea.

Thank you

Upvotes: 2

Views: 244

Answers (2)

Stefan H
Stefan H

Reputation: 6683

I believe when you declare your event, you need to give it a type to use, just like you are doing with the EventHandler

public event EventHandler<DeserializeEventArgs<T>> DeserializeEvent;

needs to be

public event EventHandler<DeserializeEventArgs<YourType>> DeserializeEvent;

Upvotes: 5

Paul Phillips
Paul Phillips

Reputation: 6259

T is a type parameter - essentially a stand-in for the actual type you're going to use. Whatever type of object you wanted passed with your Deserialize event, you should instead put its name there.

If your class is called "Data" then:

public event EventHandler<DeserializeEventArgs<Data>> DeserializeEvent;

Upvotes: 2

Related Questions