Visual studio 2010 compilation issue - visual-studio-2010

I have a project in Visual Studio 2010 with target to framework 3.5 with some code like this:
public class Test {
private object _field;
private Action defaultAction = null;
public Test(Action a)
{
defaultAction = a;
}
public Test()
: this(() => { _field = new object(); })
{
}
}
When I compile the project from the VS reports a compile error at line 11.
When I compile the project from the command line with "C:\Windows\Microsoft.NET\Framework\v3.5\msbuild.exe Test.sln" compiles successfully.
In fact this code compiles in VS2008 but in VS2010 with target to framework 3.5, doesn't.
Any idea about what is happend?
Update
To fix the code in V2010 I changed as follows (this is equivalent to origian code):
public class Test {
private object _field;
private Action defaultAction = null;
public Test(Action a)
{
defaultAction = a;
}
public Test()
{
defaultAction = () => {_field = new object();}
}
}
But what worries me is that Visual Studio is compiling my code with framework 4.0 and this may cause other errors when deploying my application in the customer's environment (framework 3.5).

I don't have access to VS2008 or .NET 3.5 at the moment so I can't investigate why it's compiling for you. There might be an explanation linked from What's New in the .NET Framework 4
What I can offer however is a fix to get it compiling in VS2010. By declaring _field as static, an instance of it will be available in the constructor of the Test class.
Replace the second line of your posted code with this and the code should compile:
private static object _field;

Related

How to Reference .Net Core Library in a .Net Core Console Application

I am following this code example
I am on Visual Studio Community 2019 for Mac. I created a .Net Core - Class Library project and compiled to create the assembly file P1-ProgramStructure.dll.
I created another solution with program2.cs code. Please see the code below.
I renamed the .dll to acme.dll and copied the file into its directory.
Class library - .Net Core Project
Program1.cs
using System;
namespace Acme.Collections
{
public class Stack
{
Entry top;
public void Push(object data)
{
top = new Entry(top, data);
}
public object Pop()
{
if (top == null)
{
throw new InvalidOperationException();
}
object result = top.data;
top = top.next;
return result;
}
class Entry
{
public Entry next;
public object data;
public Entry(Entry next, object data)
{
this.next = next;
this.data = data;
}
}
}
}
.Net Core Console App
Program2.cs
using System;
using Acme.Collections;
class Example
{
static void Main()
{
Stack s = new Stack();
s.Push(1);
s.Push(10);
s.Push(100);
Console.WriteLine(s.Pop());
Console.WriteLine(s.Pop());
Console.WriteLine(s.Pop());
}
}
When I run the project, I get the error:
$ dotnet run
Program.cs(15,7): error CS0246: The type or namespace name 'Acme' could not be found (are you missing a using directive or an assembly reference?) [/Users/csarami/VisStudioProjects/cSharp Projects/Project2-ProjectStructure/Project2-ProjectStructure/Project2-ProjectStructure.csproj]
The build failed. Please fix the build errors and run again.
Make sure both projects have the same target framework

visual studio 2015 debugger stop work, kept getting "error CS0103: The name does not exist in the current context "

I am using visual studio 2015 and created a blank mvc application using mvc core.
in startup.cs file, i added a new test method. then I just appended it to the hello world string. If I just run it, everything works. I got "Hello World! 3". but if I try to debug my code. I set a few breakpoints in the test method. when I move the mouse mover those a, b, c variables. I kept getting ": error CS0103: The name 'xxxx' does not exist in the current context". this is just happened today. and I created this brand new app, had nothing in there besides this simple method.
Here's what I did so far. restart visual studio, reset all settings. But still got the same error message. I think its the visual studio.
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
int d = test();
app.UseIISPlatformHandler();
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!" + d.ToString());
});
}
private int test()
{
var a = 1;
var b = 2;
var c = a + b;
return c;
}
// Entry point for the application.
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}
I had a very similar siutation happen in my debugger with ASP.NET Core projects as well. I was able to correct it by going to Tools > Options > Debugging > General and checking "Use Managed Compatibility Mode"

Entity Framework Designer Extension Not loading

I created a small extension for the EF designer that adds a new property to the property window. I did this using a vsix project (new project -> c# -> extensibility -> vsix project). When I hit F5 the experimental VS instance starts up. I create a new project, add an entity data model and add an entity. However, my break points never get hit and I don't see the property. Any ideas as to what I might be doing wrong?
public class AggregateRootValue
{
internal static XName AggregateRootElementName = XName.Get("AggregateRoot", "http://efex");
private readonly XElement _property;
private readonly PropertyExtensionContext _context;
public AggregateRootValue(XElement parent, PropertyExtensionContext context)
{
_property = parent;
_context = context;
}
[DisplayName("Aggregate Root")]
[Description("Determines if an entity is an Aggregate Root")]
[Category("Extensions")]
[DefaultValue(true)]
public string AggregateRoot
{
get
{
XElement child = _property.Element(AggregateRootElementName);
return (child == null) ? bool.TrueString : child.Value;
}
set
{
using (EntityDesignerChangeScope scope = _context.CreateChangeScope("Set AggregateRoot"))
{
var element = _property.Element(AggregateRootElementName);
if (element == null)
_property.Add(new XElement(AggregateRootElementName, value));
else
element.SetValue(value);
scope.Complete();
}
}
}
}
[Export(typeof(IEntityDesignerExtendedProperty))]
[EntityDesignerExtendedProperty(EntityDesignerSelection.ConceptualModelEntityType)]
public class AggregateRootFactory : IEntityDesignerExtendedProperty
{
public object CreateProperty(XElement element, PropertyExtensionContext context)
{
var edmXName = XName.Get("Key", "http://schemas.microsoft.com/ado/2008/09/edm");
var keys = element.Parent.Element(edmXName).Elements().Select(e => e.Attribute("Name").Value);
if (keys.Contains(element.Attribute("Name").Value))
return new AggregateRootValue(element, context);
return null;
}
}
EDIT: I put the code on Github: https://github.com/devlife/Sandbox
EDIT: After Adding the MEF component to the manifest as suggested, the extension still never loads. Here is a picture of the manifest:
So the answer, as it turns out, is in how I setup my project. I put both classes inside the project which produces the VSIX file. By simply moving those classes into another project and setting that project as the MEF Component in the manifest (and thus copying the assembly) it worked like a charm!
For VS2012, it is only needed to add Solution as MEF component also. Just add whole solution as MEF component also.
Then it works surprisingly fine.
It seems the dll built by your project isn't automatically included in the generated VSIX package, and VS2013 doesn't give you options through the IDE to change this (that I can work out, anyway).
You have to manually open the project file and alter the XML. The property to change is IncludeAssemblyInVSIXContainer.
Seen here: How to include VSIX output in it's package?

How to encapsulate User Setting (Options Page) in Visual Studio 2010 AddIn

I'm currently developping a Visual Studio Extension and I have a question about Options Page. Options Page allows user to save setting about your Extension. Visual Studio handle a lot of work for us.
I created the Options Page.
public class VisualStudioParameter : DialogPage
{
private string _tfsServerUrl = DefaultParameter.TfsServerUrl;
[Category("TFS Parameters")]
[DisplayName(#"Server Name")]
[Description("The URL of your TFS Server")]
public string TfsServerUrl
{
get { return _tfsServerUrl; }
set { _tfsServerUrl = value; }
}
}
First, I created a method in the Visual Studio Package to acces to the Options Page.
Okay so now, from my Package, I can easily acces to the settings.
partial class SpecFlowTfsLinkerExtensionPackage : Package : IParameter
{
....
....
public string GetTfsServerUrl()
{
return ((VisualStudioParameter) GetDialogPage(typeof (VisualStudioParameter))).TfsServerUrl;
}
}
Now, I want to be able, in another library (Another project, included in the VSIX Package), to get easily these values. I don't want to reference the Visual Studio AddIn Package in my library.
I also have Unit Test so I'm going to create an Interface. During Unit Test, I going to Mock the object.
public interface IParameter
{
string GetTfsServerUrl();
}
Do you have any idea about how I can develop a clean solution to get these parameters from another assembly ?
Do you think the better solution is to inject the AddIn dependency in my library ?
If you already developed a Visual Studio Extension, How did you encapsulated the user setting from your core assembly ?
Thanks a lot.
You can try something like that:
// Access DTE infrastructure
EnvDTE.DTE dte = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
// Access options page
var props = dte.get_Properties(#"Your Extension", "General");
var pathProperty = props.Item("TfsServerUrl");
path = pathProperty.Value as string;

creating and using DLLs in visual studio c#

I have created my first dll using c# and visual studio 2010. I am trying to use it in a program. The dll is in the program directory but visual studio will not allow using myDLL stating that it could not be found. I have also tried adding it as a reference in the solution explorer. What more do I need to do?
Here is one of the files from my class.
namespace nbt
{
class TAG_Long
{
private string name;
private long payload;
public TAG_Long(FileStream f)
{
name = NameTag.SetTagName(f);
byte[] buffer = new byte[(int)dataBytes.TYPE_LONG];
f.Read(buffer, 0, (int)dataBytes.TYPE_LONG);
Array.Reverse(buffer);
payload = BitConverter.ToInt64(buffer, 0);
}
}
}
Try putting your class in a namespace

Resources