c# - Pass Type as parameter and check -
i want build method accepts parameter type
like
void m1(type t) { // check type }
and call
m1(typeof(int));
i have no idea how check type
in method body.
i have tried
if (t double)
but giving warning
the given expression never provided type (double)
please me checking type of parameter.
if want check exact type, can use:
if (t == typeof(double))
that's fine double
, given it's struct, can't inherited from.
if want perform more is-like check - e.g. check whether type compatible system.io.stream
- can use type.isassignablefrom
:
if (typeof(stream).isassignablefrom(t))
that match if t
system.io.memorystream
, example (or if it's system.io.stream
itself).
i find myself having think work out way round call goes, target of call usually typeof
expression.
Comments
Post a Comment