My C# solution (.Net 4.0) in VS2013 includes a project defining a static class. The constructor of this static class tries to connect to a database and does some manipulation with the data.
I'm facing the problem that every time I'm trying to build my solution or I'm referencing this static class in other projects (in the same solution), VS2013 is trying to call the constructor of the static class. After calling the constructor, VS2013 gets nonresponding. Definitely, as a temporary solution, I can comment the complicated part of the constructor, but eventually I'll still have to uncomment it and try to build.
Could you please tell me how to turn off in VS2013 this option of automatic loading of static classes in design mode? Tried to google, but no success.
Thanks!
You could add a test in your static constructor and only execute your code logic if you're not in design mode.
That being said, I wouldn't recommend you to run code that accesses a database in a static constructor. The main problem is you don't really know when it's called. See https://csharpindepth.com/articles/BeforeFieldInit
Related
As the title suggests I want to query all the Startup Projects set in a IVsSolution/IVsHierarchy, also I would love to achieve that without the DTE needed. Therefor DTE2.Solution.SolutionBuild.StartupProjects isn't really a suitable option for me. I have look into the MSDN, but didn't find quite what I needed. I came across the IVsSolutionManager with its get_StartupProject method. Unfortunately it will only return a single startup project. Is this even doable with using the DTE?
It appears, the EnvDTE.Solution.SolutionBuild.StartupProjects is the only way to do this. I did a walk through of the underlying VS IDE code base, and the startup projects are denoted by an internal flag on an internal C++ class tracking all the loaded projects. Unfortunately, neither that particular class or it's m_dwStartupOpt member are directly (or indirectly exposed.
So your best/only option here is to use that StartupProjects property.
Sincerely,
I am currently debugging my application. (.Net Framework 4.6.2)
I have an instance of a class, that is somehow getting modified when performing a specific function (on debugging). I would now like to understand, what accessed and changed the object.
void PerformCalculationWithObject(MyClass obj)
{
DoSomething(obj); // calls a lot of different operations
// which sometimes modify obj, and sometimes not.
}
But the class is large and complex, and many function are called underneath the function.
I am aware that my biggest issue is the lack of good object-design in this legacy code, which leads to this problem.
So the question is, if there is a possibility to auto-break, when some member of the class (maybe even just fron specific instance) is called (without putting plenty of break-points).
If you're using VS 2019 and .NET Core 3.0 there are data breakpoints that can be set on an object from the watch window. Just right click on an expression in the watch window (in this case add a watch for obj) and then choose "Break when value changes".
More info on this is over at: https://devblogs.microsoft.com/visualstudio/break-when-value-changes-data-breakpoints-for-net-core-in-visual-studio-2019/
I appreciate this might not help you as you're debugging a legacy application but perhaps either it can run .NET Core 3 while debugging or some other user will find this answer useful.
Something I usually do in such cases is change the class member varibles which I suspect to change to properties with a get and set method and then put a breakpoint int the set method.
I am working with Visual Studio 2015. I have created a new C# console application and a new Unit Test Project. I want to connect the two of them, to be able to do unit tests. I have added a reference to the console application in the Unit Test Project. But when I try to add the using statement, it does not pop up in Intelli-Sense.
With a class library instead of a console application, this worked fine. Why doesn't this work, and how do I get it to work?
Thanks in advance
You need to create new public class in console application, where you will write your functionality, after that you need to create an instance of this class in the unit test and test the functionality.
Class Programm its an entry point which parses the arguments, so you dont need to have there any logic and also you dont need to test it.
The fix is a combination of all your answers/comments.
IntelliSense does not show the Console App. I don't know why, maybe because there is no public class yet. I can type it manually. It won't work calling main or the main class (called 'Program'). But if I create a public class with a constructor, I can call it from the unit test.
So, basically, my template works, and can now be used to build my future TDD projects on.
Thanks to you all!
I've recently had to migrate to the C# world. Coming from the Java land, I could add a public static void main(String[] args) method to any class and select to run that class from Eclipse/Netbeans for any code/logic that I wanted to quickly test.
Is there an equivalent of the same capability in C#.Net/Visual Studio? I've tried doing that and the best I can do is to execute it from the command prompt via csc.exe. However, for some reason, it complains about not finding the relevant DLLs - it seems to expect to run that class in complete isolation without any dependency on "external code" (i.e., code residing in that VS project/solution where the class resides).
Reason for this capability: All project files are marked as class libraries and sometimes I just wish to check if a particular set of methods/data/logic will work as expected with the current code base. In Java, I'd quickly write it in the main method and execute that class to see how it goes prior to committing it to version control. However, there seems to be no easy way to trigger the execution of "my class" with all dependencies correctly handled by csc.exe
Current Solution: Add this testing code to the unit test project and select to execute that particular "test" so as to check if the idea seems to work fine (it may fire DB calls or webservice class etc., and not be purely a logical flow of computation). This seems to work fine and is my current way of doing things. I was wondering if the Main method was even possible/recommended.
Question: Is this even possible with C#/VS or not recommended?
Update: I can't add a console project just to achieve this since the addition of projects is tightly controlled by the source control team. Hence the question of the Main method 'hack' for quick and dirty checks/tests.
Your project type needs to be Console Application for it to "recognize" a Program.Main method, not Class Library. The intent is for a Class Library to be an encapsulated grouping of functionality that can only be accessed by a project that is set up to allow for user input. Those can be a Console Application, Web project (MVC/API), or Desktop (WPF).
If you just want to execute a test against the code within a Class Library project, you can also create a Unit Test project, add a reference and execute very explicit tests against the functionality you're looking to achieve.
You can find out the differences between the different project types by examining the .csproj files in your favorite text editor.
In Visual Studio go New->Project then select Console Application (in Windows\Classic Desktop in VS2015). This gives you a basic console application with...
static void Main(string[] args)
{
}
setup and ready to go. However for simply trying out code you may find this cumbersome (creating a new project and folder just to test code) and for testing code (that doesn't rely on existing libraries) you could use something like .NET Fiddle...
https://dotnetfiddle.net/
Where you can quickly create and test code there and run it via the browser.
I have a C# class that has far too much code in, and I want to refactor it. What I would like to do is start with all the public methods, and build a tree for each one, showing which other methods in the class are called from it, and then which are called from the child one, and so on.
This will enable me to see which private methods belong solely to one public method, which are shared and so on.
Note that I DON'T want to do this at run time, I want to be able to look at a class, either directly at the .cs file, or using reflection on the compiled DLL.
I know I can use reflection on the compiled DLL to get the methods, but I can't find any way of finding out which methods are called by other methods in the class.
Anyone any ideas? Again, this is NOT a run time issue, it's purely to build a reusable utility to help refactor an oversized class. There are quite a few in the solution I'm working on, so the code woudl be used over and over again.
Visual Studio 2010 has action "View Call Hierarchy" where you can see all places in your solution where your code is invoked.
From my experience this static analysis may be somewhat lacking, for example method could be called dynamically using Reflection, through Data Binding, through Dependency Injection Container, etc.
Also, that may be bit off topic, and not applicable in your case, but I find a good way to find dead code for component is to have a suite of integration tests. Then you can run code coverage, and visually see what paths of code are never executed.