vb.net C# 替代 Visual Basic .NET 命名空间 My

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14172344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:47:12  来源:igfitidea点击:

C# alternative to Visual Basic .NET namespace My

c#.netvb.net

提问by Epsil0neR

I'm a VB.NET programmer, but I'm new to C# and now I'm working with C# project which uses local resource files (.resx).

我是一名 VB.NET 程序员,但我是 C# 新手,现在我正在使用使用本地资源文件 (.resx) 的 C# 项目。

Using VB.NET I can access to variables in resource file via My.Resources.< LocalResourceFile >.< MyVariable > . But in C# I can't find any alternatives for Mynamespace, but I still can access to resource if I replace Mynamespace with < MyProjectNamespace >.

使用 VB.NET,我可以通过My.Resources.< LocalResourceFile >.< MyVariable >访问资源文件中的变量。但是在 C# 中,我找不到My命名空间的任何替代方案,但是如果我将My命名空间替换为 < MyProjectNamespace > ,我仍然可以访问资源。

Maybe there are any other way to access my local resources?

也许还有其他方法可以访问我的本地资源?

回答by CC Inc

If you absolutely mustuse the equivalent of the Mynamespace, there is actually such a thing in C#. It is the Microsoft.VisualBasic.Devices.MyServicesnamespace. To use that, you must add a reference to Microsoft.VisualBasic.dll and add using Microsoft.VisualBasic.Devices;to your code file. For this route, see here: http://msdn.microsoft.com/en-us/library/ms173136.aspx

如果你绝对必须使用My命名空间的等价物,那么在 C# 中实际上有这样的东西。它是Microsoft.VisualBasic.Devices.MyServices命名空间。要使用它,您必须添加对 Microsoft.VisualBasic.dll 的引用并添加using Microsoft.VisualBasic.Devices;到您的代码文件中。有关此路线,请参见此处:http: //msdn.microsoft.com/en-us/library/ms173136.aspx

I would not, however recommend doing that. Instead, to access resources, you simply use MyNamespace.Properties.Resourceslike this:

我不会,但是建议这样做。相反,要访问资源,您只需MyNamespace.Properties.Resources像这样使用:

namespace SomeNamespace
{
...
    var myFile = SomeNamespace.Properties.Resources.MyFile;
...
}

回答by Hans Passant

It still works the same way, just don't put "My.Resources" in front of the name. So if you added a .resx file named "Resource1" and put a string named "Foo" in the resource then the variable name is "Resource1.Foo".

它仍然以相同的方式工作,只是不要将“My.Resources”放在名称前面。因此,如果您添加了一个名为“Resource1”的 .resx 文件并在资源中放置了一个名为“Foo”的字符串,那么变量名称就是“Resource1.Foo”。

If you put code in non-default namespaces then you may have to prefix it with the default project namespace. Like "WindowsFormsApplication1.Resource1.Foo". If you are lost then just look at the auto-generated code. In the Solution Explorer window open the node next to the .resx file and double-click the Designer.cs file. Don't edit it.

如果将代码放在非默认命名空间中,则可能必须使用默认项目命名空间作为前缀。像“WindowsFormsApplication1.Resource1.Foo”。如果您迷路了,请查看自动生成的代码。在解决方案资源管理器窗口中打开 .resx 文件旁边的节点,然后双击 Designer.cs 文件。不要编辑它。

回答by Caster Troy

You can do this which is virtually the equivalent:

你可以这样做,这实际上是等价的:

Bitmap image = Properties.Resources.picture000;

回答by Ofer Zelig

There is no built-in Mynamespace, but you can reference Microsoft.VisualBasic.dlland have it.

没有内置My命名空间,但您可以引用Microsoft.VisualBasic.dll并拥有它。

Check out: http://msdn.microsoft.com/en-us/library/ms173136(v=vs.80).aspx

查看:http: //msdn.microsoft.com/en-us/library/ms173136(v=vs.80).aspx