Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a class property with type T

Tags:

c#

How can I have a property which can accept object of any type (of class)... something like this?

public class HttpConnectorRequest
{
    public int Id { get; set; }
    public T RequestObject { get; set; } where T: class
    public string ResponseData { get; set; }
    public Exception Exception { get; set; }
}

I am trying to acheive an alternative for something like this:

public class HttpConnectorRequest
{
    public int Id { get; set; }
    public ClassA ClassARequestObject { get; set; }
    public ClassB ClassBRequestObject { get; set; }
    public ClassC ClassCRequestObject { get; set; }
    public string ResponseData { get; set; }
    public Exception Exception { get; set; }
}
like image 366
Alex Avatar asked May 02 '11 20:05

Alex


People also ask

How to access property in c#?

The code block for the get accessor is executed when the property is read; the code block for the set accessor is executed when the property is assigned a new value. A property without a set accessor is considered read-only. A property without a get accessor is considered write-only.

Can properties be private in c#?

Property in C# is a member of a class that provides a flexible mechanism for classes to expose private fields.

What are properties in class give example?

Properties do not name the storage locations. Instead, they have accessors that read, write, or compute their values. For example, let us have a class named Student, with private fields for age, name, and code.

What is property in C# class?

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they're public data members, but they're special methods called accessors.


1 Answers

That's fine - you just need to make it generic:

public class HttpConnectorRequest<T> where T: class
{
    public int Id { get; set; }
    public T RequestObject { get; set; }
    public string ResponseData { get; set; }
    public Exception Exception { get; set; }
}

Then you'd write something like:

var request = new HttpConnectorRequest<string>();
request.RequestObject = "Hello!";

Generics is a big topic - MSDN is probably a reasonable starting point, although I suspect you'll want to read about it in a tutorial or book at some point. (While my own book, C# in Depth, obviously covers generics, plenty of others do too :)

Note that this makes the whole type generic. If you want to make just a single property generic, you're out of luck... although you could make a method generic:

public class HttpConnectorRequest
{
    // Other members elided

    public void SetRequestObject<T>(T value) where T : class
    {
        ...
    }

    public T GetRequestObject<T>() where T : class
    {
        ...
    }
}

Quite what this would do is up to you - bear in mind that someone could write:

var request = new HttpConnectorRequest();
request.SetRequestObject<string>("Hello");
var button = request.GetRequestObject<Button>();
like image 150
Jon Skeet Avatar answered Oct 04 '22 00:10

Jon Skeet