从立即窗口执行方法 [英] Execute method from immediate window

查看:233
本文介绍了从立即窗口执行方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当应用程序未运行时,可以在Visual Studio的即时窗口中执行静态方法。



给定

 命名空间Handyman 
{
public class Program
{
static void Main(string [] args)
{

}

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

SayHello静态方法可以执行从直接窗口使用

 ?SayHello(Miki Kola)

语法,并将邮件返回到即时窗口。



我想知道是否可能使用相同的技术来执行对象的方法?当然,您必须首先创建对象。



给定

 命名空间Handyman 
{
public class NiceTooMeetYou
{
public string NiceToMeetYou(string name)
{
return string.Format(很高兴认识你{0}!,name) ;
}
}
}

when command

 ?(新的Handyman.NiceToMeetYou()。NiceToMeetYou(Miki Kola))
pre>

在立即窗口中执行

 类型或命名空间名称'NiceToMeetYou'不存在于命名空间'Handyman'

错误消息。我错过了语法或概念吗? :

解决方案

你犯了一个简单的错误:



类名为 NiceTooMeetYou (double o)。



您正在使用单个o:

 ?(新的Handyman.NiceToMeetYou()。NiceToMeetYou(Miki Kola))//单个o 

相反,这样做:

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

或将类名称更改为 NiceToMeetYou 我认为您打算做什么


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? :)

解决方案

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

这篇关于从立即窗口执行方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆