简体   繁体   中英

C# class separation into a header and a .cs file

Is there a way to separate a C# class into a header that contains the class definition and then an actual .cs file that contains the implementation? I suppose one can do this by creating an interface, but that doesn't seem right. I just want a file where I can just see the class design, and not all the detail. It's easy enough to do in C++, but I haven't seen it done with C#.

Thanks in advance.

That's a wrong approach. C# isn't C++. Forget about header files .

If you want the class summary, just open the Object Browser in Visual Studio. It will give you the signature of all the methods within your classes.

You could use partial classes and partial methods. I'm not sure why you'd do this though...

Partial Classes and Methods - MSDN

In the same file, you can use Visual Studio outline function to collapse the class so that you only see the names of methods and properties.

You can also use Visual Studio to see the Class View which gives you the names of various methods and properties of a class.

There is almost no reason in dotnet to need to define a class in a separate place from its implementation.

No there is no way (or real reason) to want to do this in C#. You can get VS.NET to summarise a class for you (collapse all in the view menu) or if you really want to as you say you can use an interface. What is the reason behind you asking?

I don't think you can do header files like you can in C++. Check out the partial keyword for both classes and methods. I just learned about them yesterday, so I haven't really used them, but they might help you accomplish what you're trying to do.

You can use an interface to achieve the same intention.

IFoo.cs:

public interface IFoo
{
  int DoFoo();
}

Foo.cs:

public class Foo : IFoo
{
  public int DoFoo()
  {
    return 1;
  }
}

Extracting the interfaces isn't a great plan if you're interested in the private methods. Using abstract classes means materially altering the design of the application (and I think, increasing complexity needlessly) to support the "view" requirement. Partial classes don't show you the complete public and private signature in one place, so that's not ideal either.

So if you don't have the IDE, or don't want to use it, I would use the default disassemble action in Reflector (free, and a great toy to have anyway):

http://www.red-gate.com/products/reflector/index.htm

eg. System.Web.Caching.Cache

public sealed class Cache : IEnumerable
{
    // Fields
    private CacheInternal _cacheInternal;
    public static readonly DateTime NoAbsoluteExpiration;
    public static readonly TimeSpan NoSlidingExpiration;

    // Methods
    static Cache();
    [SecurityPermission(SecurityAction.Demand, Unrestricted=true)]
    public Cache();
    internal Cache(int dummy);
    public object Add(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback);
    public object Get(string key);
    internal object Get(string key, CacheGetOptions getOptions);
    public IDictionaryEnumerator GetEnumerator();
    public void Insert(string key, object value);
    public void Insert(string key, object value, CacheDependency dependencies);
    public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration);
    public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback);
    public object Remove(string key);
    internal void SetCacheInternal(CacheInternal cacheInternal);
    IEnumerator IEnumerable.GetEnumerator();

    // Properties
    public int Count { get; }
    public long EffectivePercentagePhysicalMemoryLimit { get; }
    public long EffectivePrivateBytesLimit { get; }
    public object this[string key] { get; set; }
}

This was only a feature of C++ because ancient compilers needed a forward-declaration of the function signatures to work properly.

If this is something that you find handy to have though, and you want more than once, you could try writing a small utility that used reflection to extract the public interface from any component, and format it out to a text file in whatever layout you wanted.

Another alternative would be to use the /// syntax to create XML documentation for the class.

Isn't this what the IDE is for?

EDIT: Otherwise inferfaces and abstract classes is the way to go.

Try the Class View. When you click on each class you will get the members listed.

The IDE will show you exactly that inline when you have an instance followed by the "." like

myBadlyNamedObject.

(or "ClassName."), and the beauty is that you have it at your fingertips when working with the object and not when you decide to open up the object definition to see what it might be

As far as I know that's not possible. You can however make things a little bit better by using partial classes where you put different parts of a class in different files. You can for example put all public methods in one file and all private in one to make it easier to get an overview of which methods are available for use from other objects.

如果确实需要这样做,则最接近的选项是Refactor-> Extract interface。

To all you people saying "USE THE IDE~!~~", you're missing the point. Code isn't always read in the IDE. Maybe he wants it to be printed out? Or emailed? That's the problem with not implementing language features because you can get the IDE to do it: Code isn't always read (or even written) in an IDE.

That said, you can use partial classes and methods to do it; I don't think you'll be able to have instance variables in both files, however.

你总是可以使用局部类

WinCV is a .net 1.0 framework sdk utility that gives you a c++ like header view for .net assemblies. Search google for wincv.exe on how to configure it for .net 2.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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