本文介绍了C# - 从其他类访问私有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


Hello,



在'Worker'类中,'del'变量最终访问私有方法在'Program'类中(将参数发送到Program类中的Worker_WorkPerformed方法,但Worker_WorkPerformed是私有的)。但根据合同,私有方法只能从
自己的类访问。不是其他人。


如何访问私有方法?谢谢


使用System; 


namespace DelegatesAndEvents
{
public class WorkPerformedEventsArgs:EventArgs
{
public WorkPerformedEventsArgs(int hours,WorkType workType)
{
小时=小时;
WorkType = workType;
}
public int Hours {get;组; }
public WorkType WorkType {set;得到; }
}
}



使用系统; 


namespace DelegatesAndEvents
{
// public delegate int WorkdPerformedHandler(object o,WorkPerformedEventsArgs e);


公共类工人
{
公共事件EventHandler< WorkPerformedEventsArgs>工作完成;
公共事件EventHandler WorkCompleted;


public void DoWork(int hours,WorkType workType)
{
for(int i = 0; i< hours; i ++)
{
OnWorkPerfomed(i + 1,WorkType.Coffee);
}
OnWorkCompleted();
}
protected virtual void OnWorkPerfomed(int hours,WorkType workType)
{
var del = WorkPerformed as EventHandler< WorkPerformedEventsArgs> ;;
if(del!= null)
{
del(this,new WorkPerformedEventsArgs(hours,workType));
}
}
protected virtual void OnWorkCompleted()
{
var del = WorkCompleted as EventHandler;
if(del!= null)
{
del(this,EventArgs.Empty);
}
}
}
}




 
使用System;


名称空间DelegatesAndEvents
{
public enum WorkType
{
GoToMeeting,
Coffee,
Break
}
class程序
{
static void Main(string [] args)
{
worker worker = new Worker();
worker.WorkPerformed + = Worker_WorkPerformed;

worker.DoWork(10,WorkType.GoToMeeting);
}


private static void Worker_WorkPerformed(object sender,WorkPerformedEventsArgs e)
{
Console.WriteLine($" Hour is {e.Hours } - WOrk Type是{e.WorkType}");
}
}
}

工人阶级:



解决方案

Hello,

In 'Worker' class, 'del' variable eventually access to private method in 'Program' class ( send parameter to Worker_WorkPerformed method in Program class but Worker_WorkPerformed is private). but According to the contract, private method can only access from own class. Not others.

How to access private method? Thanks

using System;


namespace DelegatesAndEvents
{
    public class WorkPerformedEventsArgs : EventArgs
    {
        public WorkPerformedEventsArgs(int hours , WorkType workType)
        {
            Hours = hours;
            WorkType = workType;
        }
        public int Hours { get; set; }
        public WorkType WorkType { set; get; }
    }
}


using System;


namespace DelegatesAndEvents
{
    //public delegate int WorkdPerformedHandler(object o , WorkPerformedEventsArgs e);


    public class Worker
    {
        public event EventHandler<WorkPerformedEventsArgs> WorkPerformed;
        public event EventHandler WorkCompleted;


        public void DoWork(int hours , WorkType workType)
        {
            for (int i = 0; i < hours; i++)
            {
                OnWorkPerfomed(i + 1, WorkType.Coffee);
            }
            OnWorkCompleted();
        }
        protected virtual void OnWorkPerfomed(int hours , WorkType workType)
        {
            var del = WorkPerformed as EventHandler<WorkPerformedEventsArgs>;
            if (del != null)
            {
                del(this, new WorkPerformedEventsArgs(hours,workType));
            }
        }
        protected virtual void OnWorkCompleted()
        {
            var del = WorkCompleted as EventHandler;
            if (del != null)
            {
                del(this, EventArgs.Empty);
            }
        }
    }
}


using System;


namespace DelegatesAndEvents
{
    public enum WorkType
    {
        GoToMeeting,
        Coffee,
        Break
    }
    class Program
    {
        static void Main(string[] args)
        {
            Worker worker = new Worker();
            worker.WorkPerformed += Worker_WorkPerformed;

            worker.DoWork(10, WorkType.GoToMeeting);
        }


        private static void Worker_WorkPerformed(object sender, WorkPerformedEventsArgs e)
        {
            Console.WriteLine($"Hour is {e.Hours} - WOrk Type is {e.WorkType}");
        }
    }
}

Worker Class:

解决方案


这篇关于C# - 从其他类访问私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:03