Context:
I am building an Add-in using visual studio 2010. One of the actions is to ensure an Interface exists on a related project. If the interface does not exist, it must be created.
Checking if the interface exists, it not the problem. Creating the interface is.
I would like to create the interface using AddNewItem(), but this will only work properly on the current active project.
code:
ProjectItem item = VsProject.ProjectItems.Cast< ProjectItem >( ).FirstOrDefault( p => p.Name == interfaceName );
if ( item == null )
{
item = VsProject.ProjectItems.DTE.ItemOperations.AddNewItem( #"Visual C# Items\Code\Interface", interfaceName+".cs" );
}
Has anybody an idea?
P.S. To be clear: the Add-in is called from a different project in the same solution.
I think you'll want to use Project.ProjectItems.AddFromTemplate() instead. No trouble getting the right Project reference.
Related
I am developing an extension to Visual Studio 2015. How do I get workspace object for the current solution loaded in the IDE so that my extension can work on it?
A lot of samples seem to load a project or a solution as below, however, I want to get the workspace of the Solution loaded in the IDE so that my extension can access to it.
Dim workspace = New AdhocWorkspace()
Dim solution = workspace.CurrentSolution
Dim project = solution.AddProject("projectName", "assemblyName", LanguageNames.VisualBasic)
Dim document = project.AddDocument("name.vb", "...some code")
Roslyn defines several types of workspaces but the one you are interested in is a VisualStudioWorkspace.
You can get to it via MEF from the constructor of your vsix:
[ImportingConstructor]
public Ctor([Import(typeof(SVsServiceProvider), AllowDefault = true)] IServiceProvider vsServiceProvider, [Import(typeof(VisualStudioWorkspace), AllowDefault = true)] Workspace vsWorkspace)
Or by using the Component Service:
IComponentModel componentModel = this.serviceProvider.GetService(typeof(SComponentModel)) as IComponentModel;
var workspace = componentModel.GetService<Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace>();
You may find this question and this question useful as well.
I have a C# project in Visual Studio which has several classes under it. I am trying to run each class separately but when ever I hit the start or debug buttons,only one of the classes (the first one I created) runs.I tried right-clicking the other classes but they don't have the run option. I am using Visual Studio Express 2013
Update (To clarify the question)
Under the Solution C-SharpTutorial i have two .cs files (ArrayTest.cs and Program.cs). What am asking is if it's possible to run these files separately. Right now, I am only able to run the Program.cs file which is the first one i created.
I assume that by classes you actually mean projects. Because one Solution contains one or more projects, and projects can be run.
The answer to that is here: http://msdn.microsoft.com/en-us/library/a1awth7y.aspx
To set a single startup project
In Solution Explorer, select the desired startup project within your solution.
On the Project menu, choose Set as StartUp Project.
Otherwise, please clarify your question.
Okay, i assume you have a console application. While you cannot "run classes", you can set a startup method: https://stackoverflow.com/a/49585943/1974021
A class is a set of methods. To execute (non-static) methods, a class must be instantiated. But the runtime does not know how to call an arbitrary constructor. Therefore, the program execution starts in a static method called "Main".
If multiple classes contain a suitable Main() method, you should be able to select the desired one according to the link above.
It sounds like you mean Projects, not Classes. To change the project that is executed when you start debug mode, you can right click on the project and select "Set Active Project".
If you set breakpoints in any of the other projects that are referenced, they will still be hit and you will be able to debug using Visual Studio.
If you need to run multiple projects, you will need to run these manually from the bin\Debug folder, and then use the "Attach To Process" feature in Visual Studio to attach the debugger to those processes so that you can debug them.
Update
No, you cannot 'run' two different classes separately. A console application has only one entry point. However, if you're learning C# and testing code, you can use a switch statement.
For example:
void main()
{
Console.Write("Choose Option (1/2):");
var key = Console.ReadKey().KeyChar;
switch (key)
{
case "1":
{
var arrayTest = new ArrayTest();
arrayTest.Run();
break;
}
case "2":
{
var anotherTest = new AnotherTest();
anotherTest.Run();
break;
}
}
}
This way, when the app runs it will prompt you for a key, and you can press 1 or 2 to execute whatever you want.
With that said, for writing basic test code, I find using LINQPad significantly more productive as it bypasses the need of writing all of the boilerplate console application code.
There are 2 Reasons
Every Class have their own Main() Method
2.C# has case sensitive so method name like Main() not like main() it won't show in project properties window
---->Kept as startup project under project properties-->> Application--->
select a project which u want run
There are two types of reasons for this:
Every class has their own Main() method.
C# is case-sensitive, so method names like Main() are not like main() and won't show in the project properties window.
Solution: Keep as startup project under project properties --> Application --> select a project which you want run.
I have few questions regarding development in Visual Studio C# project for AX 2012.
There is a tool that provides Application Explorer from where you can drag any AOT item (Table, Class) in your project.
I dragged CustTable from the Application Explorer into my project and I can see the proxy class generated for it and all the methods that were in the Table are visible but I am interested to fetch all the records like below
select CustTable
So If I create object of the proxy class in Visual Studio how I will get all the records, there is one possibility to write a method in AX and call in the Visual Studio.
Second question is, I have created a class library and added in the C Sharp project of AOT, how I can use in the X++ classes? Is there anyway to call it. Please provide me some links related to it.
You can do one of the following : (assuming you have 2012 R2 by now)
You can use the new Linq provider: For sample code on how to do this, you can see here : http://msdn.microsoft.com/en-us/library/jj677293.aspx
You can use the table proxy as you mention above but this is done by using the find method on the Custtable.
CustTable custtable = new CustTable();
custtable = CustTable.findByCompany(dataAreaId, accountNum);
You could also use the business connector which has been around for a while now. An example of this is found here : http://msdn.microsoft.com/en-us/library/cc197126.aspx (This lets you use things like : axRecord.ExecuteStmt("select * from %1"); )
You can do something like this:
CustTable c = new CustTable();
c.ExecuteStmt("select * from %1");
while (c.Found)
{
MessageBox.Show(c.Name);
c.Next();
}
I am developing a Visual Studio package and I have written some code that will make a file in Solution Explorer dependant upon another file.
What this means is that it gives them the same relationship as code-behind files or designer files, where they appear nested under the parent file with a plus/minus icon.
+ MainForm.cs
- MainForm.cs
MainForm.Designer.cs
MainForm.resx
The code that I have written successfully and correctly modifies the underlying project file, however the change is not reflected in Solution Explorer until the project is closed and re-opened.
I'm looking for some code that will refresh or reload the project so that the change is visible in Solution Explorer immediately.
Further Information...
Here is the sudo code that demonstrates the mechanism by which I create the dependant file.
IVsBuildPropertyStorage vsBuildPropertyStorage = GetBuildPropertyStorage();
vsBuildPropertyStorage.SetItemAttribute(projectItemIdentifier, "DependentUpon", parentFileName);
I have also tried adding this in an attempt to get the project to reload, but it doesn't have any effect.
project.Save();
VSProject obj = project.Object as VSProject;
obj.Refresh();
AFAIK the only way of doing this is via automation of the Solution Explorer tool-window:
EnvDTE.DTE dte = ...;
string solutionName = Path.GetFileNameWithoutExtension(dte.Solution.FullName);
string projectName = project.Name;
dte.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer).Activate();
((DTE2)dte).ToolWindows.SolutionExplorer.GetItem(solutionName + #"\" + projectName).Select(vsUISelectionType.vsUISelectionTypeSelect);
dte.ExecuteCommand("Project.UnloadProject");
dte.ExecuteCommand("Project.ReloadProject");
Note that, if the project hasn't been saved, the user will get a dialog box prior to the "Project.UnloadProject" call.
Here is my code (with reactivating the old window):
public void RefreshSolutionExplorer(EnvDTE.Project activeProject, string captionOfActiveWindow)
{
DTE2 dte2 = activeProject.DTE as DTE2;
string solutionName = Path.GetFileNameWithoutExtension(dte2.Solution.FullName);
string projectName = activeProject.Name;
// Activate SolutionExplorer window
dte2.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate();
// Select your project to be updated
dte2.ToolWindows.SolutionExplorer.GetItem(solutionName + #"\" + projectName).Select(vsUISelectionType.vsUISelectionTypeSelect);
// Refresh SolutionExplorer window
dte2.ExecuteCommand("View.Refresh", String.Empty);
// Reactivate your old window
dte2.Windows.Item(captionOfActiveWindow).Activate();
}
We have a DLL project which has existed for a long time (maybe as far back as Visual Studio 6) which has been updated for each new version of VS. The project contains a number COM classes implemented using ATL.
After upgrade to VS 2010, the project still builds fine. However, if I try to right-click the project and choose Add -> Class... -> ATL Simple Object, I get an error box that says this:
ATL classes can only be added to MFC EXE and MFC Regular DLL projects or projects with full ATL support.
This worked in VS 2008.
When I look at the project properties, Use of MFC was set to Use Standard Windows Libraries and Use of ATL was set to Not Using ATL. I changed these to Use MFC in a Shared DLL and Dynamic Link to ATL respectively, but still get the same error.
I know how to add new ATL objects without using the wizard, and I could try to recreate the project from scratch using VS 2010 to make it happy. But does anyone know of any easy way to get VS to allow me to use the ATL Simple Object wizard with a project that it doesn't recognize as a project "with full ATL support"?
Check this thread out.
It seems that adding this fragment info your ATL C++ code make it work. You don't need to actually build the project, just remove this stuff away after you are done with the wizard (provided that solution works for you).
// Added fake code begins here
class CAppModule :
public CComModule
{
};
// Added fake code ends here, below is regular ATL project stuff
CAppModule _Module;
This is where it all comes from, in $(VisualStudio)\VC\VCWizards\1033\common.js:
/******************************************************************************
Description: Returns a boolean indicating whether project is ATL-based.
oProj: Project object
******************************************************************************/
function IsATLProject(oProj)
{
try
{
var oCM = oProj.CodeModel;
oCM.Synchronize();
// look for global variable derived from CAtlModuleT
var oVariables = oCM.Variables;
for (var nCntr = 1; nCntr <= oVariables.Count; nCntr++)
{
var oVariable = oVariables(nCntr);
var strTypeString = oVariable.TypeString;
if (strTypeString == "ATL::CComModule" || strTypeString == "ATL::CAutoThreadModule")
{
return true;
}
Same problem here, but the project source already had CComModule _Module;
Fixed it, based on the IsATLProject script shown above, by changing it to
**ATL::**CComModule _Module;