如我有
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要改为什么呢?或是怎么写呢?谢谢

解决方案 »

  1.   

    string myName = this.GetType().GetField("MyName").GetValue(this).ToString();
      

  2.   

    上面是手打的,有个地方打错了是
    public class UserList:UserBase 

        public string MyName{get;set}

    ----
    所以this.GetType().GetField是不行的
      

  3.   

    new UserList 使用 GetField 可以找到如果你new  UserBase   MyName就不存在!!
      

  4.   

    看语法我觉得还是this.GetType().GetProperty("MyName").这里的参数问题,没试过。。
      

  5.   

    new UserList 使用 GetProperty 可以找到 
    如果你new  UserBase  MyName就不存在!!
    -----------------------------------------
    是的,但是我的UserList引用UserBase
    所以我想他们是有关系的,就是如何通过这个关系在UserBase定位到UserList
      

  6.   


    你就是这里错了,改成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; }
        } 
      

  7.   


     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();
      

  8.   


    报空引用错误?
    楼主的例子本来就是Field,用GetField没错的,我在机子上跑过的,你自己测试 一下。
      

  9.   

    using System;
    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();        }
        }
    }这个就是可以的
      

  10.   

    using System;
    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();        }
        }
    }是空引用错误,