I have both visual studio 2017 and 2012.
My project that I'm creating working only with VS2012 (I really don't know why, there are some additional installations that can be installed only on VS2012).
Here is part of my code.
For example:
enum Colors
{
blue = 0,
green = 1,
red = 2,
}
public Class LED
{
private ComponentLED[] _arr;
public LED()
{
//Here I create the array and fill him with the objects.
}
private ComponentLED GetLEDObjectByColor(string color)
{
//This line don't work
//('System.Enum' does not contain a definition for 'Parse'
int index = (int)( (Colors)Enum.Parse(typeof(Colors), color) );
return _arr[index];
}
}
From what I understood: System.dll don't define some functions.
The only functions I see that works on Enum class are:
Enum.Equals
Enum.ReferenceEquals
So I thought the problem may be in the system.dll it's self.
Maybe you know what is the problem or how to solve it. I would very appreciate you.
Here some information about my current system.dll
Path: C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.dll
Runtime Version: v4.0.30319
Version: 4.2.0.0
and Application information:
Target Framework: .Net Micro FrameWork 4.2 (It's most - can't change it)
The .NET MicroFramework is a very, very slimmed down version of the .NET framework that can run on embedded systems. In order to fit in the tight memory limits for these systems, a lot of features of the .NET base library have been stripped to leave the most important ones only. The correct .NET MicroFramework system library guidance for enums can be found here. And you'll see that it doesn't contain any Parse options.
In the case of your Color enum parsing, you'll probably have to implement your own code to do so based on the underlying int values. That will also make it much faster on these limited systems.
The .NET MicroFramework development slowed down considerably in the past years, but recently a new team has picked up development for it and ported it to Visual Studio 2017. A nice intro can be found on Channel9 and the code to build against 2017 can be tracked in this GitHub issue.
Thank you.
So the optimal solution would be...
private ComponentLED GetLEDObjectByColor(string color)
{
if(Colors.blue.ToString() == color)
return _arr[(int)Colors.blue];
//And continue it until I passed all the enums...
}
Related
Sketch of the usage scenario:
HashSet<SomeClass> nodesToCopy = someList.Where(SomePredicate).ToHashSet();
After I have upgraded my project from the target .net framework version 4.5 to 4.5.2, the method Enumerable.ToHashSet<> is suddenly not available anymore, which gives me errors. As I look into the documentation, Microsoft tells me that Enumerable.ToHashSet has been introduced with version 4.7.2.
Now I am a little bit confused because it has definitly been there (and working) with version 4.5, because I got no errors then. I also did not define the extension method myself, which has been confirmed by searching my old code (and otherwise an extension method would also be there when I switch the framework to 4.5.2). It is also clear that I have not missed any 'using' declarations or assembly references, because otherwise it would not have worked in the first place.
So, isn't 4.5.2 downward compatible to 4.5 ? And I can hardly imagine that Microsoft is tampering with history (at least not deliberately) with regards to version 4.7.2. Maybe they had ToHashSet as a 'hidden feature' in 4.5?
While I appreciate and understand foreseeable recommendations to use newer frameworks, I cannot do this at the moment for various reasons. And I think I need to change to 4.5.2 to incorporate an external dll which is also compiled for 4.5.2. Of course, one solution would be to implement ToHashSet myself.
But my point is not primarily the solution, but I want to understand ('cause knowledge is power) if there is a sane reason for ToHashSet to once be there and then again not in a later framework. Does anyone know what is going on here?
Update 20230209: I have set up a minimal 'working' example. ToHashSet produces the desired (but unexpected, as for framework 4.5) result, no errors. Just a plain console application:
using System;
using System.Linq;
using System.Collections.Generic;
namespace ToHashSetError
{
class Program
{
public static void Main(string[] args)
{
List<string> myList = new List<string>(new[]{"abcd","efg"});
HashSet<string> myHashSet = myList.ToHashSet();
Console.Write(String.Join(",", myHashSet));
Console.Write("\r\nPress any key to continue . . . ");
Console.ReadKey(true);
}
}
}
with the default assembly references SharpDevelop chooses, and with target framework 4.5 set:
What is remarkable, the same code when trying to compile it on a different PC, fails as expected with an error (ToHashSet not available) for framework 4.5. So it is obviously something rooted in the jungle of different framework installations. But how to fix this, or even just find out the reason?
This may be a long shot... I need to create a simple-ish web app that needs to call an OEM supplied SDK that is 10-15, or more, years old (no updates are possible). The SDK is 32bit and is pegged to .Net Framework 3.5. There is still an active NDA for using the SDK so there is not much that I can share.
I have access to Visual Studio 2015, 2017 and 2019. In each version I can go through the process of creating a C# web app and I can select .Net 3.5 as the target framework but the only available template is a "Blank" one. Are there MVC based templates for 3.5? I have googled and have not found any.
That is my first problem. I decided to just move on and try newer .Net versions that might have a more complete template set. I have tried 4.8 most recently.
Using .Net 4.8 and specifying an "x86" platform I can create a simple app. I can call the SDK lib and get values from the simple library "get" methods like "getHeight", "getWidth", etc. The SDK is for an old computer controlled device that still works like a charm. It has a camera and one of the available functions is GetCameraImage.
The SDK comes with documentation and some sample C# "SLN"s. One of those has the following sample code:
var idata = new byte[numOfBytes];
unsafe {
fixed (byte* fixedPtr = idata) {
channel.GetCameraImage((int) numOfBytes, out idata[0]);
}
}
In my code the above just crashes with an "Access Violation". No exceptions are thrown in the debugger. It just dies.
The docs that come with the SDK suggest that the following is valid:
var idata = new byte[numOfBytes];
channel.GetCameraImage((int) numOfBytes, out idata[0]);
but I get the same Access Violation.
Any ideas about the Access Violation?
EDIT
#Serg: Addressing a couple of comments: var idata = new byte[numOfBytes*3] did not work but was a great idea.
#DekuDesu: I am not sure how to exactly verify that the memory is allocated so I did the following:
var idata = new byte[numOfBytes];
for (var i=0;i<numOfBytes;i++) {
idata[1] += 0;
}
channel.GetCameraImage((int) numOfBytes, out idata[0]);
I also tried this with #Serg's offset idea and the violation is definately triggered by the GetCameraImage call.
how do I make Contract work on macOS?
the following code
private static int GetInt(string s)
{
Contract.Requires<ArgumentNullException>(s != null);
return 10;
}
static void Main()
{
Console.WriteLine(GetInt("s"));
}
leads to the exception:
An assembly (probably "TestConsoleApp") must be rewritten using the code contracts binary rewriter (CCRewrite) because it is calling Contract.Requires and the CONTRACTS_FULL symbol is defined. Remove any explicit definitions of the CONTRACTS_FULL symbol from your project and rebuild. CCRewrite can be downloaded from http://go.microsoft.com/fwlink/?LinkID=169180. \r\nAfter the rewriter is installed, it can be enabled in Visual Studio from the project's Properties page on the Code Contracts pane. Ensure that "Perform Runtime Contract Checking" is enabled, which will define CONTRACTS_FULL.
it's 2020 today, I work with dot.net core on my mac, but
the link from above is for downloading windows installer that was implemented 5 years ago in 2015 and works with Visual Studio 2010, 2012, 2013, 2015.
And I just wonder if public static class Contract is something that Microsoft uses internally or obsolete or something...
I hope someone can shed some light on the matter, can't you? ))
you were asking "I just wonder if public static class Contract is something that Microsoft uses internally or obsolete or something".
As per my understanding, it's unfortunately obsolete (it used to work with VS 2015 as you pointed out, provided you installed an extension).
See:
Does Visual Studio 2017 work with Code Contracts?
You could perhaps try PostSharp's version of Code Contracts?
I hope that MS will continue support of code contracts sometime in the future.
I have a Windows application which captures the details from screen based on the configuration. I am using UI Automation to capture the details from the screen. Everything works fine on the developer's machine where Visual Studio is installed. When I run the same application on another system where we have only .NET Framework 4.5 installed, it started behaving strangely, and it's not able to detect the child element.
My question is why it works fine on the developer's machine where Visual Studio and .NET Framework are installed. What's the difference? Is there anything we are missing as far as prerequisites? Any dependencies of UI Automation or any library we are missing..?
Thanks in advance - please help me out.
It looks like a known bug in .NET wrapper around native UIAutomationCore.dll (yes, its core is not a .NET). And it's included into WinVista+ (.NET Framework also adds it even to WinXP).
Here is a C# example how to use native COM API (UIAutomationCore.dll) from C#. Just copying the code here:
using System;
using interop.UIAutomationCore;
namespace PrintDesktopUiaElementNameViaCom
{
class PrintDesktopUiaElementNameViaComProgram
{
static void Main(string[] args)
{
// Instantiate the UIA object:
IUIAutomation _automation = new CUIAutomation();
// Get the root element
IUIAutomationElement rootElement = _automation.GetRootElement();
// Get its name
string rootName = rootElement.CurrentName;
Console.WriteLine(
"The root automation element's name should be 'Desktop'.");
Console.WriteLine("The actual value is: '{0}'", rootName);
}
}
}
Yeah at last after doing day's reading, i came to know the solution is that der is no dependency on Visual studio.
This behavior is due to lack of privileges to the application. so to overcome this behavior we have to get signed our application and one more thing its very important thing is place your executable file in Program Files
Reference Links : https://msdn.microsoft.com/en-us/library/windows/desktop/ee671610(v=vs.85).aspx
I'm trying to evaluate an expression stored in a database i.e.
"if (Q1 ==2) {result = 3.1;} elseif (Q1 ==3){result=4.1;} else result = 5.9;"
Rather than parsing it myself I'm trying to use the DLR. I'm using version .92 from the Codeplex repository and my solution is a .NET 3.5 website; and I'm having conflicts between the System.Core and Microsoft.Scripting.ExtenstionAttribute .dll's.
Error =
{
Description: "'ExtensionAttribute' is ambiguous in the namespace 'System.Runtime.CompilerServices'.",
File: "InternalXmlHelper.vb"
}
At this time I cannot upgrade to .NET 4.0 and make significant use of the .net 3.5 features (so downgrading is not an option).
Any help greatly appreciated.
The solution is to type forward ExtensionAttribte into System.Core.dll. We've made 3 different versions of this assembly (for the 3 different versions that we've shipped w/ various IronPython versions) and attached to them on this bug on IronPython's CodePlex site.
You'll need to download them and check the versions on them and replace the one that matches the version in the CodePlex release you're using.
I might be to complex thinking right now and more easy solutions exists, but this jumped into my mind as a possibility;
Have you considered building a runtime class using the CodeDom, instanciating one, executing a method on it (with the expression as its implementation, which is more like code-snippets than a pure expression) and then retrieving the 'result' value from that class instance via a public property?