public function genericcast(of u, v)(byval obj as u) as v
return ctype(obj, v)
end function
return directcast(directcast(obj, object), v)
return (v)(object)obj;
public function genericcast(of u, v)(byval obj as u) as v
return ctype(directcast(obj, object), v)
end function
static v genericcast<u, v>(u obj)
{
iconvertible convertibleobj = obj as iconvertible;
if (convertibleobj != null)
{
type t = typeof(v);
switch (type.gettypecode(t))
{
case typecode.boolean:
return (v)(object)convertibleobj.toboolean(null);
case typecode.byte:
return (v)(object)convertibleobj.tobyte(null);
case typecode.char:
return (v)(object)convertibleobj.tochar(null);
//.........
default:
//none of them, use the following default way..
break;
}
}
return (v)(object)obj;
}
static v genericcast<u, v>(u obj)
{
return (v)convert.changetype(obj, typeof(v));
}
public function genericcast(of u, v)(byval obj as u) as v
try
return ctype(directcast(obj, object), v)
catch ex as invalidcastexception return directcast( _
compilerservices.conversions.changetype(obj, gettype(v)), v)
end try
end function
新闻热点
疑难解答
图片精选