简体   繁体   English

C#静态类与VB.NET模块的使用

[英]C# Static Class vs VB.NET Module usage

I'm having difficulty calling a function from a C# Class in a similar way that I call a function from a VB.NET Module. 我很难以类似于从VB.NET模块调用函数的方式从C#类调用函数。

I have a C# class General.cs 我有一个C#类General.cs

  using System;

  namespace XYZ.Classes
     {
     public static class General
     {
         //Object Null to Empty Function
         public static string NullToEmpty(object obj)
         {
             var returnString = "";
             if (obj != null)
             {
                 returnString = obj.ToString();
             }
             return returnString;
         }

     }
  }

This type of function can be called in VB.NET from anywhere in the project without declaring the module or prefixing the call - just using 可以在VB.NET中从项目的任何位置调用此类型的函数,而无需声明模块或在调用前添加前缀-只需使用

  dim x as String = NullToEmpty(obj) - VB.NET

  var x = NullToEmpty(obj) - C#

From my googling, it seems a public static class with public static methods may be able to accomplish this in c#. 从我的谷歌搜索来看,带有公共静态方法的公共静态类似乎可以在c#中完成此任务。

C# Example: C#示例:

class foo.cs foo.cs类

  namespace XYZ.Classes
  {
      public class Foo
      {

          public string doFoo()
          {
               var obj = null;
               var foolish = NullToEmpty(obj);
               return foolish;
          }
       }
   }

The function shows up in intellisense (using ReSharper) - but it's not valid(red), so something is not referenced correctly - I'm just guessing... 该功能以智能感知(使用ReSharper)显示-但无效(红色),因此未正确引用某些内容-我只是在猜测...

The point is being able to simply use common 'User Defined' utility functions for null trapping, formatting, etc... - so as not to have to wrap all kinds of stuff in ugly C# code like this: 关键是能够简单地使用通用的“用户定义的”实用程序函数进行空陷阱,格式化等...-从而不必将各种东西包装在丑陋的C#代码中,例如:

  obj.FooField = dr["Foo"] == null ? "" : dr["Foo"];

Would prefer: 希望:

  obj.FooField = NullToEmpty(dr["Foo"]);

This becomes even more useful for DateTime applications: 这对于DateTime应用程序变得更加有用:

  obj.ActivityStartDate = dr["ActivityStartDate"] == null ? "" : Convert.ToDateTime(dr["ActivityStartDate"]).ToString("yyyy-MM-dd HH:mm:ss");

vs: VS:

  obj.ActivityStartDate = GetDate(dr["ActivityStartDate"]);

Or Integer Conversion: 或整数转换:

  cmd.Parameters["@BirthdayDay"].Value = String.IsNullOrEmpty(obj.BirthdayDay) ? 01 : Convert.ToInt32(obj.BirthdayDay);

vs: VS:

  cmd.Parameters["@BirthdayDay"].Value = NullToZero(dr["obj.BirthdayDay"]);

Some C# guru must know this :-) 一些C#专家必须知道这一点:-)

Thanks 谢谢

在类中访问静态方法要求您包括所有者类

obj.FooField = General.NullToEmpty(dr["Foo"])

My experience has been that you have a few options: 我的经验是您有几种选择:

1) Embed the utility function in a base class that all of your other classes inherit from. 1)将实用程序功能嵌入所有其他类都继承的基类中。 This is not really a practical solution because you will probably have classes that inherit from third party or .Net framework classes, which would be very difficult to modify. 这实际上不是一个实际的解决方案,因为您可能会有从第三方或.Net框架类继承的类,这将很难修改。

2) Use Extension methods to extend your functionality to existing base classes. 2)使用扩展方法将您的功能扩展到现有的基类。 I think that this would end up being more work than it's worth. 我认为这最终将花费更多的精力。

3) Make a very simple-named global method holder class (ie Util) and just prefix your methods with this hold name. 3)创建一个非常简单的全局方法所有者类(即Util),并使用该保留名称作为方法的前缀。

We used option 3 to convert a large VB application to C# and it worked quite well. 我们使用选项3将大型VB应用程序转换为C#,并且效果很好。 Although it isn't quite a convenient as the VB syntax, once you get used to it, it becomes very easy to work with. 尽管它不像VB语法那样方便,但是一旦习惯了,它就变得非常容易使用。

VB.NET adds methods in a Module to the global (unnamed) namespace. VB.NET将模块中的方法添加到全局(未命名)名称空间。 This is mostly a back-compat feature, if the team could have removed module support then they would probably have done so. 这主要是反向兼容功能,如果团队可以删除模块支持,那么他们可能会这样做。 But they couldn't, it would have made it too difficult to port VB6 code to VB.NET. 但是他们不能,将VB6代码移植到VB.NET太困难了。 The practice is iffy and doesn't scale at all, you tend to run into trouble when the project becomes large. 这种做法很不稳定,根本无法扩展,当项目规模很大时,您往往会遇到麻烦。 A problem know as "global namespace pollution". 一个称为“全局名称空间污染”的问题。

Programmers tend to work around it by giving method names a prefix. 程序员倾向于通过给方法名称加上前缀来解决它。 Which works but is pretty awkward since you have to cough up that prefix every time you want to call the method, there is no analogue of the Imports directive for that. 哪个可行,但是很尴尬,因为每次您要调用该方法时都必须咳嗽那个前缀,对此没有与Imports指令类似的东西。 IntelliSense suffers greatly as well. IntelliSense也遭受很大的痛苦。

C# just doesn't permit doing this at all. C#根本不允许这样做。 Closest you could get is with extension methods. 您最能得到的就是扩展方法。 But don't, get used to the C# Way. 但是,请不要习惯C#方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM