public abstract class Vehicle
{
protected void SomeMethod<T>(String paramName, ref T myParam, T val)
{
//Get the Type that myParam belongs to...
//(Which happens to be Car or Plane in this instance)
Type t = typeof(...);
}
}
public class Car : Vehicle
{
private String _model;
public String Model
{
get { return _model; }
set { SomeMethod<String>("Model", ref _model, value); }
}
}
public class Plane: Vehicle
{
private Int32 _engines;
public In32 Engines
{
get { return _engines; }
set { SomeMethod<Int32>("Engines", ref _engines, value); }
}
}
Is it possible to do what I’m looking for… that is, get t be typeof(Car) or typeof(Plane) using the referenced parameter myParam somehow?
Oh, and I would like to avoid having to pass in a ‘this’ instance to SomeMethod or adding another Generic constraint parameter if I can.
You don’t need to pass in
this– it’s already an instance method.Just use:
That will give the actual type of vehicle, not
Vehicle.