English 中文(简体)
C#: 参数属于哪类
原标题:C#: Get the Type that the parameter belongs to
  • 时间:2010-08-12 17:06:26
  •  标签:
  • c#
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); }
    }
}

难道我能做些什么? 即,利用参考参数我的帕拉姆某种方式,获得某种类型(碳)或类型(Plane)?

奥赫,我想避免在这样的情况下通过一些Method,或者如果我能够的话增加另一个通用的限制参数。

最佳回答

无需在<条码>上通过——它已经是一种实例方法。

公正使用:

Type t = this.GetType();

这将使actual 车辆类型,而不是<代码>。

问题回答

您可打电话,在目前情况下运行。 这种做法产生一点误导性,因为法典存在于基类中,但在操作时,它会正确地获得你所继承的类别。

Type t = this.GetType(); 
/*or equivlently*/
Type t = GetType();

在一旁注上,你不必将这一类型通过<代码>。 一些Method将由汇编者提交。

public class Car : Vehicle 
{ 
    private String _model; 
    public String Model 
    { 
        get { return _model; } 
        set { SomeMethod("Model", ref _model, value); } 
    } 
} 

<代码>SomeMethod non-generic and 然后Type t = this.GetType()





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签