Generics and member access

  • Thread starter Bruno Neves Pires Silva
  • Start date
B

Bruno Neves Pires Silva

Hello, Programmers.

How can I access a member of an object using generics?
I've got the following problem:I Have a class that uses generics like
below:

class ClassName<typename>
{
public void method(typename var)
{
var.Method2(); //I've got an error here.
}
}

How can I Access Method2 from the class typename ???

in VB.Net its easy:

Class ClassName(Of typename)
public sub method(byval var as typename)
Dim obj As Object = var
obj.Method2()
end sub
End Class

Thanks in advance.
 
M

Marc Gravell

C# 3 enforces that the method must resolve at compile-time (C# 4 may
change this). Either you need a constraint that provides this (such as
"typename : ISomeInterface" where ISomeInterface defines Method2()),
or you need to use reflection, defeating the purpose of generics. You
could also use a base-class in place of the interface, but interfaces
are generally more versatile (due to single type-inheritance in .NET).

Marc
 
M

Marc Gravell

C# 3 enforces that the method must resolve at compile-time

(and all prior, of course...)
 
A

Alberto Poblacion

Bruno Neves Pires Silva said:
Hello, Programmers.

How can I access a member of an object using generics?
I've got the following problem:I Have a class that uses generics like
below:

class ClassName<typename>
{
public void method(typename var)
{
var.Method2(); //I've got an error here.
}
}

How can I Access Method2 from the class typename ???

One way to do it is by means of a constraint on typename:

class ClassName<typename> where typename: myInterface

where myInterface is an interface or base class that declares Method2.
Now the compiler knows that Method2 is a member of typename, and therefore
when you declare var of type typename, it knows that it can call Method2 on
var.
in VB.Net its easy:
[...]
It's easy, but not safe or efficient, only if you have Option Strict
Off. If you set Option Strict to On, it will complain the same way as C#,
and the remedy is the same (add a constraint).
 
J

Jon Skeet [C# MVP]

How can I access a member of an object using generics?

Did you ask the same question recently? I'm sure someone did.
I've got the following problem:I Have a class that uses generics like
below:

class ClassName<typename>
{
public void method(typename var)
{
var.Method2(); //I've got an error here.
}

}

Given that var is a contextual keyword in C# 3, it's not a great
variable name to use. Likewise conventionally "typename" would be T.
Just a suggestion to make it easier for others to understand your
code.

Basically, you need to constrain the type parameter, e.g.

class ClassName<T> where T : IDisposable
{
public void Method(T item)
{
item.Dispose(); // Fine, because T implements IDisposable
}
}
in VB.Net its easy:

Try doing it with Option Strict On, and I suspect you'll find it
doesn't work.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top