一个用户类:using System;namespace SystemFrameworks
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class User
{
private int userID; private string userName; private string password; public int UserID
{
get
{
return this.userID ;
}
set
{
this.userID = value;
}
} public string UserName
{
get
{
return this.userName ;
}
set
{
this.userName = value;
}
} public string Password
{
get
{
return this.password ;
}
set
{
this.password = value;
}
} public User(int userID,string userName,string password)
{
this.userID = userID;
this.userName = userName;
this.password = password;
}
}
}
一个集合类:
using System;
using System.Collections;namespace BusinessRules
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Collection : CollectionBase
{
public object this[int index]
{
get
{
return (object)List[index];
}
set
{
List[index] = value;
}
} public Collection()
{
} public int Add(object values)
{
return List.Add (values);
} public int IndexOf(object values)
{
return List.IndexOf (values);
} public void Insert(int index,object values)
{
List.Insert (index,values);
} public void Remove(object values)
{
List.Remove (values);
} public bool Contains(object values)
{
return List.Contains(values);
}
}
}
测试:
User cs1 = new User(1,"zhaolin","123456");
User cs2 = new User(2,"zhlinao","123456");
User cs3 = new User(3,"linzhao","123456");
Collection cn = new Collection ();
int a = 126;
string s = "abcd";
cn.Add (s);
cn.Add (a);
cn.Add (cs1);
cn.Add (cs2);
cn.Add (cs3);
Console.Writeline((((cn[0]).GetType())(cn[0])).ToString()); //报错:应输入方法名称请大家多多帮忙,在线等待!
谢谢!
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class User
{
private int userID; private string userName; private string password; public int UserID
{
get
{
return this.userID ;
}
set
{
this.userID = value;
}
} public string UserName
{
get
{
return this.userName ;
}
set
{
this.userName = value;
}
} public string Password
{
get
{
return this.password ;
}
set
{
this.password = value;
}
} public User(int userID,string userName,string password)
{
this.userID = userID;
this.userName = userName;
this.password = password;
}
}
}
一个集合类:
using System;
using System.Collections;namespace BusinessRules
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Collection : CollectionBase
{
public object this[int index]
{
get
{
return (object)List[index];
}
set
{
List[index] = value;
}
} public Collection()
{
} public int Add(object values)
{
return List.Add (values);
} public int IndexOf(object values)
{
return List.IndexOf (values);
} public void Insert(int index,object values)
{
List.Insert (index,values);
} public void Remove(object values)
{
List.Remove (values);
} public bool Contains(object values)
{
return List.Contains(values);
}
}
}
测试:
User cs1 = new User(1,"zhaolin","123456");
User cs2 = new User(2,"zhlinao","123456");
User cs3 = new User(3,"linzhao","123456");
Collection cn = new Collection ();
int a = 126;
string s = "abcd";
cn.Add (s);
cn.Add (a);
cn.Add (cs1);
cn.Add (cs2);
cn.Add (cs3);
Console.Writeline((((cn[0]).GetType())(cn[0])).ToString()); //报错:应输入方法名称请大家多多帮忙,在线等待!
谢谢!
这样出错么?
cn[0].GetType().ToString()
他好像要把cn[0]拆箱成自己的类型,然后再TOSTRING()cn[0].ToString()不就得了 他自己会转的
中
override public string ToString()
{
return this.UserName;
}
{
return List.Add (values);
}
应该为
public int Add(User values)
{
return List.Add (values);
}
也就是说
我并不知道传进来的值是什么类型
所以才要
((cn[0]).GetType())(cn[0])
数据类型转换
我写那个User类是作为测试用的
我想用Collection来装载所有的类型
在具体使用的时候,想取得他们的具体数据类型
比如说cn[0]的数据类型为string
cn[1]的数据类型为int
cn[2]的数据类型为User
谢谢你们的支持
然后直接cn[0].ToString();
所有的对象都继承自Object, 所以都有ToString()方法;
if(o is int) {
}
if(o is string) {}
if(o is user){}
}
但遗憾得很
我所写的所有的东西都必须运行在1.x环境下
因此此路不通foreach(object o in cn){
if(o is int) {
}
if(o is string) {}
if(o is user){}
}
如果这样的话
那我其不是要判断所有可能的数据类型?
那还有什么通用性呢?再次谢谢大家的支持请问怎么样用反射和XML来做O/R映射?
请大家多多支持!
(类型名)实例,这种写法中如果用(实例.GetType())代替(类型名)就报错。不会影响到后面的功能实现吧。该干什么还是可以干什么,在必要的时候转换成特定的类型。不然就是利用虚函数和多态了。