有什么问题?返回 实例化的 一个 OBJECT !!!直接 点出来就OK!object a = get();int i = a.a;
这个匿名类型怎么去获取它的值?看得不太明白。。上面的代码我是简化了,原先的代码是 public object Get() { int a = 0, b = 0; //这里执行sql查询语句ExecuteScalar,把得到的值赋给a,b //a = 值1,b =值2 return new int[]{ a = a, b = b }; }
JavaScript 里面的匿名类。用到.NET里面还真是不习惯。
public void Run() { var v = get(3 , 5); var v2=v.GetType().GetProperties(); foreach (var item in v2) { Console.WriteLine("{0}",item.GetValue(v,null)); } Console.Read(); } object get(int a,int b) { return new { A=a,B=b,C=a*b}; } // 3 5 15
public object Get() { int a = 10, b = 20; return new { a = a, b = b }; } public void CallMethod() { List<int> listInt = Get() as List<int>; MessageBox.Show(string.Format("The Object Index:{0},Value:{1}.",0,listInt[0]); MessageBox.Show(string.Format("The Object Index:{0},Value:{1}.",1,listInt[1]); }
List<object> keyval = Get();
string[] str=new string [keyval.Count]; int i = 0; foreach (object s in keyval) { str[i++] = s.ToString(); }
你看看这个: var v = new { a = a, b = b }; v.a=90; v.b=23;实际上v是个类型,匿名类型,(而不是上面有的人说的数组) 由于object是所有类的基类,所以object o=new { a = a, b = b };也是可行的 但是转变为object后,无法再转回去,因为不知道类型的名字,所以这样的转换是无意义的另外Var匿名类型不能作全局变量,所以他的生存期在定义它的函数内,所以不能出现 public var Get()的函数形式总结,var转换为object是可以的,但会失去访问匿名类型的功能,无意义转换
这里也回答一下吧,通过反射获取属性值: protected void Page_Load(object sender, EventArgs e) { var a = Get(); Response.Write(GetValue(a, "a", true)); Response.End(); } public object Get() { int a = 10, b = 20; return new { a = a, b = b }; } public static object GetValue(object obj, string propertyName, bool IgnoreCase) { object l_ret; BindingFlags flags = BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance; if (IgnoreCase) flags |= BindingFlags.IgnoreCase; PropertyInfo info = obj.GetType().GetProperty(propertyName,flags); //BindingFlags.IgnoreCase | //PropertyInfo info = obj.GetType().GetProperty(propertyName, BindingFlags.); //object l_tmp = obj.GetType().InvokeMember(propertyName, BindingFlags.GetProperty, null, obj, null); if (info != null) { l_ret = info.GetValue(obj, null); } else { l_ret = null; } return l_ret; }
{
int a = 10, b = 20;
return new int[]{ a = a, b = b };
}
我已经试过了,但出错:异常详细信息: System.InvalidCastException: 无法将类型为“<>f__AnonymousType0`2[System.Int32,System.Int32]”的对象强制转换为类型“System.Int32[]”。
行 20: int[] aa = (int[])Get();
http://msdn.microsoft.com/zh-cn/library/bb397696.aspx
这个类有两个property
分别是
a,b
public object Get()
{
int a = 0, b = 0;
//这里执行sql查询语句ExecuteScalar,把得到的值赋给a,b
//a = 值1,b =值2
return new int[]{ a = a, b = b };
}
var v = get(3 , 5);
var v2=v.GetType().GetProperties();
foreach (var item in v2) {
Console.WriteLine("{0}",item.GetValue(v,null));
}
Console.Read();
}
object get(int a,int b) {
return new { A=a,B=b,C=a*b};
}
//
3
5
15
{
int a = 10, b = 20;
return new { a = a, b = b };
}
public void CallMethod()
{
List<int> listInt = Get() as List<int>;
MessageBox.Show(string.Format("The Object Index:{0},Value:{1}.",0,listInt[0]);
MessageBox.Show(string.Format("The Object Index:{0},Value:{1}.",1,listInt[1]);
}
List<object> keyval = Get();
string[] str=new string [keyval.Count];
int i = 0;
foreach (object s in keyval)
{
str[i++] = s.ToString();
}
var v = new { a = a, b = b };
v.a=90;
v.b=23;实际上v是个类型,匿名类型,(而不是上面有的人说的数组)
由于object是所有类的基类,所以object o=new { a = a, b = b };也是可行的
但是转变为object后,无法再转回去,因为不知道类型的名字,所以这样的转换是无意义的另外Var匿名类型不能作全局变量,所以他的生存期在定义它的函数内,所以不能出现
public var Get()的函数形式总结,var转换为object是可以的,但会失去访问匿名类型的功能,无意义转换
protected void Page_Load(object sender, EventArgs e)
{
var a = Get();
Response.Write(GetValue(a, "a", true));
Response.End();
}
public object Get()
{
int a = 10, b = 20;
return new { a = a, b = b };
}
public static object GetValue(object obj, string propertyName, bool IgnoreCase)
{
object l_ret; BindingFlags flags =
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.Instance;
if (IgnoreCase)
flags |= BindingFlags.IgnoreCase; PropertyInfo info =
obj.GetType().GetProperty(propertyName,flags);
//BindingFlags.IgnoreCase |
//PropertyInfo info = obj.GetType().GetProperty(propertyName, BindingFlags.);
//object l_tmp = obj.GetType().InvokeMember(propertyName, BindingFlags.GetProperty, null, obj, null);
if (info != null)
{
l_ret = info.GetValue(obj, null);
}
else
{
l_ret = null;
}
return l_ret;
}
加 linq 的引用 和 namespace