I can refactor a big code to a new method which automatically sets the parameters, return type, if static or not etc in the same class. Is there a way that I can move the code to a new function in a different class if I could specify the class somehow? May be I'm missing something silly.
In VIsual Studio there is no possibility to move methods between classes. But if you use ReSharper from JetBrains, you can move static methods between classes with Move To Another Type command.
Related
I heavily use the VS 2019 quick refactorings, particularly the "Generate Type in new file" refactoring.
However, this refactoring generates code with private members like this
private FloorId id;
private BuildingId buildingId;
private SiteMapId siteMapId;
whereas I want public properties like
public FloorId Id{get;}
public BuildingId BuildingId{get;}
Public SiteMapId SiteMapId{get;};
Also the class is generated with an internal access modifier on the class itself, and I want public.
I can't determine how to customise the code generation, so I thought I would create a custom VSIX Refactoring Project and build my own refactoring implementation.
I would like to try and find the source code for the "Generate class in new file" refactoring so that I can use that as a basis to build my own.
Does anyone know where I could find this code, or something similar?
It seems like creating custom refactorings VSIX projects is a rather niche subject, and not a great deal out there to work from.
If you're unaware the "Generate new type..." option that's last in the refactoring list, that pops up a dialog that lets you customize some behavior, including the access modifier of the class. We don't let you choose properties but that's a good suggestion.
The C# engine for Visual Studio (Roslyn) is open source, so you can find at least a tiny bit of the code here: https://github.com/dotnet/roslyn/tree/master/src/Features/Core/Portable/GenerateType. That said, there's a lot of code there and a huge amount of stuff it depends on...this is not trivial to rewrite and easily extract. You're probably best off trying to modify that and then running a custom Roslyn to experiment with. You may also want to consider opening an issue or discussion on the Roslyn repository, because honestly this might just be a feature we'd take via pull request.
We currently have multiple .NET projects that we have developed using Visual Studio. Each project has a reference to a shared dll that contains some helper classes.
Let's say I have an instance class named 'Helper1' with a public method named 'Save()'. Does anyone know of a way for me to search a group of projects to determine which projects/line the Save() method belonging to the Helper1 class is being invoked?
Basically, I want a list of where the Helper1 class' Save() method is being used among multiple projects.
This is not as simple as doing a text search. There could be many other classes in these projects that also have a Save() method which is being invoked, but I don't care about them. I only want to know about the Save() method belonging to the Helper1 class.
This means, the tool performing the search needs to be smart enough to understand the current namespace it is searching in. When a Save() method is found, the search tool needs to determine if the Save() method belongs to the Helper1 class or to some other class.
Note: There is no dynamic dependency injection happening in these projects, so we know at compile time which classes are being used.
Load all the dlls into "reflector" (the free version is fine)
navigate to the method you are interested in
bring up the analyser (ctrl+r I believe)
job done
Red Gate's .NET Reflector can do this, even in the free version.
The "Analyze" feature will tell you where specific types are used, exposed, and instantiated:
If you find yourself needing something a little bit more high powered, you might look into Tom Carter's Dependency Structure Matrix Plugin. This works with Reflector to give you a more powerful way of tracking inter-module dependencies. You can read an article about it here.
I am fairly new to development, and Visual Studio 2010.
I have a solution with a Test Project:
In my solution, there are two projects: "TPS" and "TPS.Tests"
In the TPS project, in the namespace "TPS.Models" I have defined a bunch of classes, and two interfaces.
I have created a Test Class in the TPS.Tests project, and have added "using TPS.Models;"
I attempt to implement the interface by typing it out (e.g. public class FakeObjectClass : IObjectClass), but it isn't recognised (so I can't get the auto-implement going, which would be handy as I have over 100 methods)
Typing in the class, in Intellisense, I can see all the objects defined in my model, but none of the interfaces.
Google has been unusually silent on the search combinations I have tried. I am hoping there is some simple explanation/fix?
Thanks in advance for your time.
Tim.
If you haven't specified an access modifier when defining the Interface, it will default to internal and not be visible to other assemblies.
Make sure that you defined your interface as
public interface IMyInterface
A few things to check:
- Does your "TPS.Tests" project have a reference to the "TPS" project?
- Are your interfaces in the "TPS.Models" namespace? Putting the files in a sub-directory of the project, such as "Interfaces", can affect their namespace.
- Are the interfaces marked as Public?
Also, I would suggest using an Isolation (aka "Mocking" framework) to create your fake objects, such as Moq, Rhino.Mocks, etc, rather than rolling your own fake objects for most situations.
Visual Studio includes a refactoring function called "Extract Interface" that generates an interface based on a class implementation.
The extracted interfaces are Internal by default. Problem is, we end up changing nearly all of them to Public.
Does anyone know why it's Internal by default? Better yet, is there a way to customize this to default to Public?
It might work if you change the Visual Studio template for interfaces (I haven't tried that but assume this should work).
For Visual Studio 2008 the template is stored at
"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates
\CSharp\Code\1033\Interface.zip"
This is described in more detail in this answer.
I don't have a reference, but I have a wild guess as to why it is internal by default.
Let's say you have 3 projects / assemblies: log4net (a 3rd party API), MyApp.Util, and MyApp.Web (a Web project). Web references Util, which references log4net. Web does not reference log4net, and you want to keep it that way.
Inside of DAL, say you have an internal class, and one of its members references a type defined in log4net. It could be the return type or one of the parameter types of a method, or the type of a property.
Let's say you extract an interface from the aforementioned class, including the aforementioned member that references log4net. Well, if you make that member public (part of a public interface) and reference a type that implements it, you then require that the Web project reference log4net.
By making the interface internal, Web may continue to be ignorant of log4net.
How do I create a COM dll using Visual Studio 2008? What are the custom settings needed for creating the dll? That dll should be used in Microsoft Navision(ERP PACKAGE).
Simply create a class library. In the project properties, open the dialog where you can change assembly information and mark the "Make assembly COM-visible" checkbox (sorry, don't know the exact name of the option, using German VS 2008).
Then, add the following attributes to any class that should be used from Navision:
[ClassInterface(ClassInterfaceType.AutoDual)]
[ProgId("YOUR-ID-GOES-HERE")]
[ComVisible(true)]
[Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]
public class ComVisbleClass
{
....
}
I suggest that you also manually assign Disp-IDs to the public methods and properties using the DispId attribute. Otherwise, inserting new public methods or properties may break Navision functionality, as the Disp-IDs may be changed upon compilation.
Navision would then refer to the old Disp-IDs which may now "point" to different methods. This is a PITA to debug and solve, so use the DispId attribute from the start.