English 中文(简体)
采用在另一类已经透视的护法
原标题:Calling a Method using a string in another class that has already been instantiated
  • 时间:2012-01-12 22:08:22
  •  标签:
  • c#
  • winforms

我面临的问题是,我要从扼杀中提一个方法。 在这方面,我正在做些什么:

我的“努力”清单中有三个不同的核对箱子。

private List<string> MyTest = new List<string>();
private void AddSelectedMethods()
{
        foreach(XName item in BaseTestList.CheckedItems)
        {
            MyTests.Add(item.ToString());
        }
        foreach (XName item in AdminTestList.CheckedItems)
        {
            MyTests.Add(item.ToString());
        }
        foreach (XName item in SubscriberTestList.CheckedItems)
        {
            MyTests.Add(item.ToString());
        }
}

这里是骑士。 如果我接受“反思”的号召,并直接提及这种方法,但我不想打造一个巨大的清单,列出其他发言。

    private void StartSiteTest(object sender, DoWorkEventArgs e)
    {
        if (!BackWorker1.CancellationPending)
        {
            if (SiteToTest == "estatesales.vintagesoftware.local" || SiteToTest == "localhost")
            {
                es = new EstateSaleTests(site, Sites.First(i => i.SiteUrl == SiteToTest), BasePath, SiteToTest, UseCurrentCompanies);
                foreach (string test in MyTests)
                {

                    // <<<!!!!!!!!  ------ The next line returns null ------ !!!!!!!>>>
                    MethodInfo thisMethod = es.GetType().GetMethod(test);

                    thisMethod.Invoke(es, null);
                }
            }
        }
    }

我在做错做的事情上的任何帮助都会受到高度赞赏。

! EDIT——!

页: 1 我把这几类人列入地狱名单,但我不得不改称我的方法。 这种方法是公开的,我刚刚必须将其重新命名为正确的名称。

最佳回答

The call you use seems pretty acceptable, imo. The thing is that GetType().GetMethod() is able to recover only public methods.

See this MSDN link.

为了与不同的变体接触,使用GetMethod(string, BledFlags)超载。

希望这一帮助。

问题回答

我注意到这一点已经得到了很好的答复,但以下可能仍然有用。

有时很难用反思来找到方法。 你们目前只是寻求公共审理方法。 在通过思考找到方法时,我通常要做的是使用<条码>GetMethods(,有不同的约束旗帜,并直接检查预期的方法是否存在。

请注意,在你指定具有约束力的旗帜时,请must 并注明: 旗帜。 InvokeMethod « BledFlags.Instance。 此外,考虑如下:

  • If the method is an instance method, use BindingFlags.Static
  • If you don t know whether you have the caption right ("CalcRoot" is different than "calcRoot") than use BindingFlags.IgnoreCase
  • If you think the method is protected, internal, private or protected internal, use BindingFlags.NonPublic
  • If you are not sure whether you use a derived type, use BindingFlags.FlattenHierarchy
  • If you are uncertain whether what you are looking for is a property, field or method, use GetMembers instead.

You can combine all flags with | to search for everything. With a little bit trial and error you ll eventually find the set of binding params that you need.





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签