如我有
public class UserBase
{
public string GetMyName()
{
string myName = this.GetType().GetProperty("MyName").GetValue(this, null).ToString();
}
}public class UserList:UserBase
{
public string MyName="123";
}
----------------
如上面这样不行
会报:未将对象引用设置到对象的实例。
因为在UserBase中的this没有办法报到UserList的MyName
那这个this要改为什么呢?或是怎么写呢?谢谢
public class UserBase
{
public string GetMyName()
{
string myName = this.GetType().GetProperty("MyName").GetValue(this, null).ToString();
}
}public class UserList:UserBase
{
public string MyName="123";
}
----------------
如上面这样不行
会报:未将对象引用设置到对象的实例。
因为在UserBase中的this没有办法报到UserList的MyName
那这个this要改为什么呢?或是怎么写呢?谢谢
解决方案 »
- AD用户怎么查找自己有哪些权限?
- 哪位大侠指教下GridView中点击编辑不会出现更新和取消为什么?
- DataGrid中表头的合并
- ajax.net库做的无刷新连动放在虚拟主机上会不会有问题
- 单击datagrid某一行的按鈕,怎样查找该行的其它控件?
- IIS里边看不到smtp虚拟服务
- web services 中使用 session 的问题
- 如何取得字段中所有不重复的绑定到DropDownList,并给DropDownList一个非绑定的默认值
- ADO.NET如何使一个数值型字段的值为空
- 如何在使用treeview在另一个frameset中打开一个页面?
- 怎么在源码模式插入UserControl
- 如何通过ajax获取.aspx生成的图片
public class UserList:UserBase
{
public string MyName{get;set}
}
----
所以this.GetType().GetField是不行的
如果你new UserBase MyName就不存在!!
-----------------------------------------
是的,但是我的UserList引用UserBase
所以我想他们是有关系的,就是如何通过这个关系在UserBase定位到UserList
你就是这里错了,改成Property就对了,你再试试: public class UserBase
{
public string GetMyName()
{
string myName = this.GetType().GetProperty("MyName").GetValue(this, null).ToString();
return myName;
}
} public class UserList : UserBase
{
public UserList()
{
MyName = "123";
} public string MyName { get; set; }
}
public class UserBase
{
public string GetMyName()
{
object obj = this.GetType().GetProperty("MyName").GetValue(this, null)??"";
return obj.ToString();
}
} public class UserList : UserBase
{
public string MyName { get; set; } public UserList()
{
MyName = "Init";
}
}
UserList ul = new UserList();
ul.GetMyName();
报空引用错误?
楼主的例子本来就是Field,用GetField没错的,我在机子上跑过的,你自己测试 一下。
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml;
using System.Collections;namespace CSharpTest
{
public class UserBase
{
public string GetMyName()
{
string myName;
myName = this.GetType().GetProperty("MyName").GetValue(this, null).ToString();
return myName;
}
} public class UserList : UserBase
{
public string MyName { get; set; }
public UserList()
{
MyName = "Init";
} }
class Program
{
static void Main(string[] args)
{
UserList u = new UserList();
u.GetMyName(); }
}
}这个就是可以的
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml;
using System.Collections;namespace CSharpTest
{
public class UserBase
{
public string GetMyName()
{
string myName;
myName = this.GetType().GetField("MyName").GetValue(this).ToString();
return myName;
}
} public class UserList : UserBase
{
public string MyName { get; set; }
public UserList()
{
MyName = "Init";
} }
class Program
{
static void Main(string[] args)
{
UserList u = new UserList();
u.GetMyName(); }
}
}是空引用错误,