Execute method from immediate window

别说谁变了你拦得住时间么 提交于 2019-12-13 13:24:53

问题


It is possible to execute static method from immediate window in Visual Studio when the app is not running.

Given

namespace Handyman
{
    public class Program
    {
        static void Main(string[] args)
        {

        }

        static string SayHello(string name)
        {
            return string.Format("Hello {0}!", name);
        }
    }
}

SayHello static method can be executed from immediate window using

?SayHello("Miki Kola")

syntax and would return the message to the immediate window.

I'm wondering if it's possible to execute method on object using the same technique? You'd have to create the object first, of course.

Given

namespace Handyman
{
    public class NiceTooMeetYou 
    {
        public string NiceToMeetYou(string name)
        {
            return string.Format("It is nice to meet you {0}!.", name);
        }
    }
}

when command

?(new Handyman.NiceToMeetYou().NiceToMeetYou("Miki Kola"))

is executed in immediate window

The type or namespace name 'NiceToMeetYou' does not exist in the namespace 'Handyman'

error message is presented. Am I missing the syntax or the concept? :)


回答1:


You have made a simple mistake:

The class name is NiceTooMeetYou (double o).

And you are calling with a single o:

?(new Handyman.NiceToMeetYou().NiceToMeetYou("Miki Kola")) //Single o

Instead, do it like this:

?(new Handyman.NiceTooMeetYou().NiceToMeetYou("Miki Kola")) //Double o

Or change the class name to NiceToMeetYou which is I think what you intended to do



来源:https://stackoverflow.com/questions/32934635/execute-method-from-immediate-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!