简体   繁体   English

局部类可以在C#中访问静态方法吗?

[英]Can partial class access static methods in C#?

The tutorial application, MusicStore -MVC3 (92 PageNo) , created a POCO class like: 教程应用程序MusicStore -MVC3(92 PageNo)创建了一个POCO类,例如:

public partial class ShoppingCart
    {
        MusicStoreEntities storeDB = new MusicStoreEntities();

        public static ShoppingCart GetCart(HttpContextBase context)
        {
            var cart = new ShoppingCart();
            cart.ShoppingCartId = cart.GetCartId(context);
            return cart;
        }
     }

How can we access static method in partial classes? 我们如何在局部类中访问静态方法? In my opinion, we cannot access static methods in a partial class. 我认为,我们不能在局部类中访问静态方法。 The partial attribute means other parts of the class will be included in the namespace. partial属性表示该类的其他部分将包含在名称空间中。 In this scenario, I do not know where this other partial class is implemented. 在这种情况下,我不知道其他部分类在哪里实现。

My questions about this static method are: 我对此静态方法的疑问是:

  1. Can we access static methods in partial classes? 我们可以在局部类中访问静态方法吗? If so, how? 如果是这样,怎么办?
  2. Where is this partial class implemented in this MusicStore application? 此MusicStore应用程序中的部分类在哪里实现? I am not able to find the other part of this class's implementation. 我找不到该类实现的另一部分。

Updated: There is no other ShoppingCart class in the models directory. 已更新: models目录中没有其他ShoppingCart类。 Does anyone know where that partial implementation would be? 有谁知道部分实现将在哪里?

A partial class in C# can definitely access static methods. C#中的partial类绝对可以访问static方法。 The partial attribute simply says a class can (not must ) be defined accross multiple files and otherwise doesn't affect member lookup. partial属性只是说一个类可以 (未必须 )被翻过多个文件中定义的和以其他方式不影响成员查找。

EDIT Responding to comment in question 编辑回应有问题的评论

A possible explanation for why you can't find the other implementation of ShoppingCart is it may not exist. 为什么找不到ShoppingCart的其他实现的一个可能解释是它可能不存在。 A partial class is not required to have multiple definitions. 一个partial类不要求有多种定义。 The partial only means there may be other parts of the definition. partial表示定义中可能存在其他部分。

  1. Yes, you can access static methods in partial classes. 是的,您可以在部分类中访问静态方法。

    Partial classes are just a way of representing a regular class in multiple source files, often with some of those source files controlled (or generated) by tools. 局部类只是在多个源文件中表示常规类的一种方式,通常这些源文件中的某些受工具控制(或生成)。

    You can call ShoppingCart.GetCart(context) anywhere - it's a normal public static method. 您可以在任何地方调用ShoppingCart.GetCart(context) -这是一种常规的公共静态方法。

  2. It's still not really clear what your second question means, but there doesn't have to be another part at all. 仍然不清楚您的第二个问题是什么意思,但是根本没有必要再讨论其他问题。 It's fine (though unusual) to have a partial class which is only declared in a single file. 拥有只在单个文件中声明的局部类是很好的(尽管很不寻常)。

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

相关问题 扩展方法中的C#静态类访问 - c# static class access in extension methods C# 为什么我可以通过属性访问非 static 类的方法和属性,而无需先创建该 class 的实例? - C# Why can I access a non static class' methods and properties via a property, without creating an instance of that class first? C#:Child类可以限制对Parent类方法的访问吗? - C#: Can a Child class restrict access to Parent class methods? 在c#中访问泛型类的静态方法 - Accessing Static Methods on a Generic class in c# 如何对自动生成的 C# 部分 class 中的现有方法进行调整? - How can tweaks to existing methods in an auto-generated C# partial class be persisted? 使用 static 方法中的 public 关键字从 C# 中的另一个 class 访问它 - Using the public keyword in static methods to access it from another class in C# 我可以导入一个 static class 作为命名空间来调用它的方法而不在 ZD7EFA19FBE7D3972FDZ5ADB6 中指定 class 名称吗? - Can I import a static class as a namespace to call its methods without specifying the class name in C#? C# - 部分 class 中的私有 static 方法不在文件之间共享? - C# - private static method in partial class not shared between files? C#-访问另一个类方法 - C# - Access another class methods 如何在C#中使用静态对象定义实现局部类 - How to implement a partial class with static object definition in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM