加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在C#中传递当前实例的引用

发布时间:2020-12-15 18:22:22 所属栏目:百科 来源:网络整理
导读:例如像(参考这个)不起作用的东西……例如这失败了: using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace CopyOfThis{ class Program { static void Main(string[] args) { View objView = new View(); objView.B
例如像(参考这个)不起作用的东西……例如这失败了:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CopyOfThis
{
    class Program
    {
        static void Main(string[] args)
        {
            View objView = new View();
            objView.Boo();
            objView.ShowMsg("The objView.StrVal is " + objView.StrVal);
            Console.Read();
        }
    } //eof Program


    class View
    {
        private string strVal;
        public string StrVal
        {
            get { return strVal; }
            set { strVal = value; } 

        }
        public void Boo()
        {
            Controller objController = new Controller(ref this);
        }

        public void ShowMsg ( string msg ) 
        {
            Console.WriteLine(msg); 
        }


    } //eof class 

    class Controller
    {
        View View { get; set; } 
        public Controller(View objView)
        {
            this.View = objView;
            this.LoadData();
        }

        public void LoadData()
        {
            this.View.StrVal = "newData";
            this.View.ShowMsg("the loaded data is" + this.View.StrVal); 
        }
    } //eof class 

    class Model
    { 

    } //eof class 
} //eof namespace

解决方法

这已经是一个参考.代码就像
DoSomethingWith(this);

将对当前对象的引用传递给方法DoSomethingWith.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读