• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C# Generics

promposive

Senior member
Is there a way to allow access to a Type's methods without implementing some kind of interface like I have defined?
The problem is I have a class that I am trying to use generics on, and I need access to one of the methods on the given type(s) (the method will exist on all the types supported by this class).

My only solution so far is to make all the Type's that would be passed into as IObjectServerType implement an interface, ITestInterface. It seems weird though, considering the IObjectServerType's are already interfaces...

I am new to generics, but I am guessing an interface is the only way to do it. Mainly just asking to see if there is anything else because that would be extremely useful. 🙂


Edit:
The code block isnt working right?
 
The code block hasn't ever worked right as far as I remember.

You're headed in the right direction by using constraints on the template parameter. Constraints are really the only way of telling the C# compiler what types it is going to have to deal with when that template is instantiated. You say the 'IObjectServerTypes' are already interfaces, but there is no way for the compiler to know that. In the declaration...

class A<T> {}

T is just a placeholder. The compiler doesn't know anything about the type of T. In that context it will only allow the methods declared by Object, since everything supports them. So if you have...

class A<T> { public A(T t) { t.Read(); } }

The compiler is going to complain because the 'T' type doesn't have a Read() method that the compiler knows about.

That's where constraints come in. You've already shown that you know how they are used. You say that IObjectServerType is already an interface. So use constraints to guarantee that to the compiler. You don't necessarily have to create a new interface. If all the types you will be using with the template implement a particular interface, then use a constraint to tell the compiler that the only types that can be substituted for IObjectServerType are types that implement that interface. If there is more than one interface, then use multiple constraints.

Bottom line: if you know that all the types needed in a certain situation should implement a certain set of methods, then you should have a base class or interface that they derive from that guarantees they can fulfill that role.

Edit: see http://msdn2.microsoft.com/en-us/library/d5x73970.aspx for description of constraints.
 
yea this is what I thought, all the types are seperate interfaces, which all have the 1 common method (update (param)) with a different type going into the param, so I guess the only way is going to be a generic interface for all the interfaces similiar to the code i posted and make all the existing interfaces implement the new ITestInterface<T>
 
Originally posted by: Dhaval00
Did you toy around with the idea of creating a base class with virtual methods or is it not applicable?

I am not exactly sure how this would help, or at least be any better than the interface solution I am thinking of.

Thanks for your reply though, as many ideas as possible is always useful.
 
Originally posted by: puffpio
how about using reflection to grab that class's Update method and call it that way

This may be an option, but am not sure yet. It would certainly be easier than implementing an interface on an existing large amount of interfaces.
I just started using some reflection today to build a config file on the program load instead of modifying it everytime a class is added to the project. It deffinately seems like a nice feature that I am going to have to look into more.
 
Originally posted by: C0BRA99
yea this is what I thought, all the types are seperate interfaces, which all have the 1 common method (update (param)) with a different type going into the param, so I guess the only way is going to be a generic interface for all the interfaces similiar to the code i posted and make all the existing interfaces implement the new ITestInterface<T>

There's your answer. That one thing that they all share defines the base class they should all derive from, or the interface they should all implement. This is a key aspect of object-oriented programming.
 
Back
Top