viena....你说的我都知道。 是这样的。我从数据库中查出了一条记录,该记录的值我通过 rowRecords[0][item]取得。 这个值我要赋给已知对象的某个属性。 DataRow[] rowRecords = selTable.Select(strExpr); if (rowRecords.Length > 0) { foreach (DataColumn item in selTable.Columns) { PropertyInfo fi = deviceItem.GetType().GetProperty(item.ColumnName); if (fi != null) { if (!(rowRecords[0][item] is DBNull)) { fi.SetValue(deviceItem, rowRecords[0][item], null); } } } } 问题是处在,数据库得出的object对象的类型可能和属性PropertyInfo 不匹配。 这就需要我认为去转 比如我现在用的是oracle,rowRecords[0][item]的值虽然是11,但是是decimal类型的,而我的PropertyInfo 的类型是int32. 如果用if判断,我也会,问题就是我想考虑全局的。 因为我知道PropertyInfo 的Type可以取到,就问下有没有方法。。
好象在做ORM 如果允许类型不匹配的话,恐怕会影响效率的 我自己的ORM要求类型必须严格匹配
C# 复制代码 using System; using System.Reflection; using System.Windows.Forms;public class A { public virtual int method () {return 0;} }public class B { public virtual int method () {return 1;} }class Mymethodinfo { public static int Main() { Console.WriteLine ("\nReflection.MethodInfo"); A MyA = new A(); B MyB = new B(); // Get the Type and MethodInfo. Type MyTypea = Type.GetType("A"); MethodInfo Mymethodinfoa = MyTypea.GetMethod("method"); Type MyTypeb = Type.GetType("B"); MethodInfo Mymethodinfob = MyTypeb.GetMethod("method"); // Get and display the Invoke method. Console.Write("\nFirst method - " + MyTypea.FullName + " returns " + Mymethodinfoa.Invoke(MyA, null)); Console.Write("\nSecond method - " + MyTypeb.FullName + " returns " + Mymethodinfob.Invoke(MyB, null)); return 0; } } 这个例子更详细些 1)GetMethod 2)Invoke
{
object i;
string s = "abc";
Type T = s.GetType();
if (T.FullName == "System.String")
{
i = Convert.ToString(s);
MessageBox.Show(i.ToString());
}
}
(FullName)object是编译时的,而不是运行时的
主要是我要写个公共的东西,如果都用已知的类型去判断,这个比较麻烦
我就想知道有没有好的方法
你可以直接把它赋值给一个此Type类型的变量而如果object实际指向的不是Type的对象
那才是类型转换的问题,视具体类型要有具体的转换代码
比如字符串转整形要进行字符串解析
还是建议多看看基础书再来~
是这样的。我从数据库中查出了一条记录,该记录的值我通过 rowRecords[0][item]取得。
这个值我要赋给已知对象的某个属性。
DataRow[] rowRecords = selTable.Select(strExpr);
if (rowRecords.Length > 0)
{
foreach (DataColumn item in selTable.Columns)
{
PropertyInfo fi = deviceItem.GetType().GetProperty(item.ColumnName);
if (fi != null)
{
if (!(rowRecords[0][item] is DBNull))
{
fi.SetValue(deviceItem, rowRecords[0][item], null);
}
}
}
}
问题是处在,数据库得出的object对象的类型可能和属性PropertyInfo 不匹配。
这就需要我认为去转
比如我现在用的是oracle,rowRecords[0][item]的值虽然是11,但是是decimal类型的,而我的PropertyInfo 的类型是int32.
如果用if判断,我也会,问题就是我想考虑全局的。
因为我知道PropertyInfo 的Type可以取到,就问下有没有方法。。
下面的示例获取名为 MethodA 的方法。C# 复制代码
using System;
using System.Reflection;class Program
{ // Method to get:
public void MethodA() { }
static void Main(string[] args)
{ // Get MethodA()
MethodInfo mInfo = typeof(Program).GetMethod("MethodA");
Console.WriteLine("Found method: {0}", mInfo); }
}
不就那几种类型吗?switch咯
版主混淆概念了吧?拆、装箱不等于类型转换。装箱从形式上来说是把值类型的数据转换成System.Object、System.ValueType,如果是枚举也可以是System.Enum,如果实现了某个接口,可以转换成对应接口,仅此4种(因为值类型不支持继承)从本质上来说,是把堆上的数据存到栈上去。这里顶多是个类型转换,根本没有提到是值类型还是引用类型,所以拆、装箱无从谈起。
是这样的。我从数据库中查出了一条记录,该记录的值我通过 rowRecords[0][item]取得。
这个值我要赋给已知对象的某个属性。
DataRow[] rowRecords = selTable.Select(strExpr);
if (rowRecords.Length > 0)
{
foreach (DataColumn item in selTable.Columns)
{
PropertyInfo fi = deviceItem.GetType().GetProperty(item.ColumnName);
if (fi != null)
{
if (!(rowRecords[0][item] is DBNull))
{
fi.SetValue(deviceItem, rowRecords[0][item], null);
}
}
}
}
问题是处在,数据库得出的object对象的类型可能和属性PropertyInfo 不匹配。
这就需要我认为去转
比如我现在用的是oracle,rowRecords[0][item]的值虽然是11,但是是decimal类型的,而我的PropertyInfo 的类型是int32.
如果用if判断,我也会,问题就是我想考虑全局的。
因为我知道PropertyInfo 的Type可以取到,就问下有没有方法。。
如果允许类型不匹配的话,恐怕会影响效率的
我自己的ORM要求类型必须严格匹配
using System;
using System.Reflection;
using System.Windows.Forms;public class A
{
public virtual int method () {return 0;}
}public class B
{
public virtual int method () {return 1;}
}class Mymethodinfo
{
public static int Main()
{
Console.WriteLine ("\nReflection.MethodInfo");
A MyA = new A();
B MyB = new B(); // Get the Type and MethodInfo.
Type MyTypea = Type.GetType("A");
MethodInfo Mymethodinfoa = MyTypea.GetMethod("method"); Type MyTypeb = Type.GetType("B");
MethodInfo Mymethodinfob = MyTypeb.GetMethod("method"); // Get and display the Invoke method.
Console.Write("\nFirst method - " + MyTypea.FullName +
" returns " + Mymethodinfoa.Invoke(MyA, null));
Console.Write("\nSecond method - " + MyTypeb.FullName +
" returns " + Mymethodinfob.Invoke(MyB, null));
return 0;
}
}
这个例子更详细些
1)GetMethod
2)Invoke
对象实际类型没有发生变化,只不过从object引用,改为了其本身类型的引用
何来类型转换?
如果有非支持的转换其会报异常,对于这种你就必须用 Convert<Tinput,Toutput>或者fun<T,Tresult>
自己提供转换支持了
本人特别不喜欢if或者switch。。呵呵~
本人特别不喜欢if或者switch。。呵呵~
Form frm=(Form)con;
在你看来,这不是类型转换?对象虽然没转换,但是引用发生了变化,这在你看来不叫转换?
这位老兄还是先看看《你必须知道的net》把,知道new的时候都发生了什么事吗??如果编译的时候就确定了,new的时候还需要层层冒泡去在内存分配父类的成员方法表吗???按你的说法,编译的时候就确定了,难道你心里想的就像博客园那位“吉日”兄一样,认为程序运行前,系统会把所有的对象类型的方法表一口气载入内存,明显不是如此的,方法表虽然在编译的时候被固定的结构,但是他实际是临时动态滴加载到内存里的
好了不说了,再说就和lz这个帖子偏题太远了
ps:提示lz一下,并不需要你去写太多if else, 你所要做的事情微软已经做了Convert.ChangeType方法就是,他提供了net里默认的常用类型的互译。按你的需求来看,实际这些常用过程就够用了
不好意思,是我说的不清楚,我说的编译是指JIT编译。不是编译成IL代码的时候。
# 0000003c mov ecx,esi
# 0000003e cmp dword ptr [ecx],ecx
# 00000040 call FFB2C050
# 00000045 nop 这篇文章挺不错,看过多次,推荐,深入探索.NET框架内部了解CLR如何创建运行时对象:http://www.microsoft.com/china/MSDN/library/netFramework/netframework/JITCompiler.mspx?mfr=true
x.GetType().FullName;
object o=(object)x;
var v=o as X;