|
Matthew Dennis wrote:
MyEnum myEnum = new MyEnum(); One slight simplification:
MyEnum myEnum = default;
MyEnum myEnum = 0;
var myEnum = default(MyEnum); default value expressions - C# reference | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
something about using 'new on an enum ... gives me a creepy feeling
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Ah yes, I agree. The problem is that we were trying to put the extension on the wrong type. It should be and extension of string.
using System;
namespace EnumConvert
{
public enum MyEnum
{
None = 0,
First,
Second,
Third,
Last
}
public static class EnumExtensions
{
public static Tenum ToEnum<Tenum>(this string str, bool ignoreCase = false)
where Tenum : struct, Enum
{
if (Enum.TryParse<Tenum>(str, ignoreCase, out Tenum result))
return result;
throw new ArgumentException($"{str} is not a valid string for conversion to {typeof(Tenum).FullName}");
}
}
class Program
{
static void Main(string[] args)
{
var none =("None").ToEnum<MyEnum>();
Console.WriteLine($"'None' results in {none} with a value of {(int)none}");
var first = "First".ToEnum<MyEnum>();
Console.WriteLine($"'First' results in {first} with a value of {(int)first}");
var last = "last".ToEnum<MyEnum>(true);
Console.WriteLine($"'last' results in {last} with a value of {(int)last}");
try
{
last = "last".ToEnum<MyEnum>(false);
Console.WriteLine($"'last' results in {last} with a value of {(int)last}");
}
catch
{
Console.WriteLine("Error trying to convert 'last' to MyEnum");
}
try
{
var random = "Random".ToEnum<MyEnum>(true);
Console.WriteLine($"'Random' results in {random} with a value of {(int)random}");
}
catch
{
Console.WriteLine("Error trying to convert 'random' to MyEnum");
}
}
}
}
"Time flies like an arrow. Fruit flies like a banana."
|
|
|
|
|
Hi Matthew, in my post on QA [^], I published a method that extends 'string with the comment: Quote: imho, you do pay a price for either one: the first example extends string; I think extending basic types is generally a mistake. In the second example, you pay a price for the rather awkward structure of the calling format. i explored this more esoteric approach ... extending an Enum ... because i try to avoid extending basic types, like 'string ... a personal preference.
Glad to have your input, and hope to hear more from you, cheers, Bill
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Extending primitive has no runtime penalty. Extension methods are a compiler syntactic sugar, so I use them a lot. The nice thing is that they are only visible if the dll containing them is included, and the file has the appropriate using statement, so only the parts of the app that need them have access to them. The intellisense list doesn't get unnecessarily polluted.
For example, public bool string.IsValidEmailAddress() is a nice extension.
"Time flies like an arrow. Fruit flies like a banana."
|
|
|
|
|
my only objection is that once you define an extension method for a basic type, putting a dot after any instance of that type at design time in VS will cause intellisense to present the method in its list. in the list of methods, the extension method is not indicated with any special visual adornment.
a more serious potential issues is the inherent 'public accessibility of extension methods ... if abused, this can violate encapsulation. you can address that, however, by the use of NameSpaces.
while there actually is a difference in the order in which extension methods are compiled: i see no reason to be concerned about that.
as i said, it's a personal preference. for a stronger opinion: [^]
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
minor quibbles: while I am aware of the newer features:
where T : Enum
use of 'default
they are not as simple to use as one might expect:
1. where T : struct, Enum ... fails without 'struct, and 'struct must precede 'Enum
2. 'default cannot be used directly ... WeekDays.Default throws an error ... however this will work:
((WeekDays) default).ToEnum<weekdays>("friday", true);
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
BillWoodruff wrote: 1. where T : struct, Enum ... fails without 'struct, and 'struct must precede 'Enum
Yes, for some odd reason an Enum constraint doesn't automatically imply a struct constraint.
As a result, it's possible to use where TEnum : class, Enum , which will compile but can never be satisfied.
But it's still better than not having the constraint.
BillWoodruff wrote: 2. 'default cannot be used directly ... WeekDays.Default throws an error
The default literal can be assigned to a variable of any type, or used as the default value of an optional parameter of any type.
It doesn't add a new member to the enum, so WeekDays.default won't work if the WeekDays enum doesn't already contain a default entry.
If you want to use the default value without assigning to a variable, you can use default(WeekDays) instead.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
reading between the lines, i almost had the feeling you thought theses comments were relevant
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
I have a couple of static methods that attempt to establish the correct enum value from the specified string (or int). I use them when loading data from a source that could potentially be corrupted by manual modifications by fat-fingered idiot users.
public static T IntToEnum<T>(int value, T defaultValue)
{
T enumValue = (Enum.IsDefined(typeof(T), value)) ? (T)(object)value : defaultValue;
return enumValue;
}
public static T StringToEnum<T>(string value, T defaultValue)
{
T enumValue = (Enum.IsDefined(typeof(T), value)) ? (T)Enum.Parse(typeof(T), value) : defaultValue;
return enumValue;
}
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
i would not use Enum.IsDefined: [^], [^]
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
I've never had a problem with that code.
It should return false if the spelling/case is wrong.
If it makes you feel more comfortable, you could change the code to use enum.TryParse instead of enum.Parse .
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
modified 22-Apr-20 13:59pm.
|
|
|
|
|
I share information with no expectation anyone will use it. Since this is a language discussion forum, not QA, I believe "going deep" is appropriate.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
modified 23-Apr-20 4:20am.
|
|
|
|
|
I wasn't complaining.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I didn't interpret your response as complaining ... my crystal ball can't read Texan minds
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Does anyone know how to add the sas.dll to a c# app?
|
|
|
|
|
Yes.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Succinct.
To the point.
Accurate.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
Hi all, I have a net core WebAPI running on a Linux box on my local LAN it all works as expected until I try to access it from outside my Lan.
What I've done
Forwarded the listening port on my router to the local ip and port on my Lan where the WebAPI resides.
Result when calling a controller method
404 not found, the API is using Nginx as a reverse proxy and I'm thinking maybe this is not configured to accept requests from the outside ( but this a guess ) - any ideas guys ?
Please let me know if I haven't supplied enough information
"We can't stop here - this is bat country" - Hunter S Thompson - RIP
modified 21-Apr-20 7:50am.
|
|
|
|
|
You shouldn't need to configure anything on your WebAPI to make it work externally vs Internally.
If the endpoint your trying to hit works from the local lan, then it should work from any source that hit's the correct API, if your getting a 404 AND your sure the traffic is getting from the outside to the inside and actually hitting your web API then it's because the routing in the dotnet app is not working.
If the traffic is getting to the internal IP, but NOT being redirected to the WebAPI, then the problem is with your forwarding server, most likely the server is trying to handle the request instead of proxying it out to the required destination.
The FIRST thing to check is the log files for your proxy forwarder, if your using Nginx then these should normally be in something like /var/log/nginx on a linux server (Can't help if it's winyhoos sorry), you should try "tail -f <logfilename>" in an ssh window while you throw a request at the IP so you can see it in real time.
If the access log shows activity, then you don't have your proxy forward configured correctly, but you don't have any config errors either.
If the error log shows activity, then you have a configuration error and the error log should tell you what to do.
My gut feeling from your description is that your going to see activity on your access log, not on your error log....
But I'll leave that until your reply....
Shawty
|
|
|
|
|
Hi Peter - I figured it out - I was forwarding to the wrong port - I wrongly thought I should forward to the port my service runs on - but of course nginx is in the mix and listens on port 80 - I added another http server {} block to my nginx config file which listens for the remote port and bingo it worked. I need to study what you sent me.
"We can't stop here - this is bat country" - Hunter S Thompson - RIP
|
|
|
|
|
Glad you got it sorted
This stuff was my bread and butter for quite a few years from 2004 to 2009, I worked primarily as a network infrastructure engineer here in the UK for one of the large 4 Mobile Telco companies
Fun times.....
|
|
|
|
|
Just for completeness I'd like to deploy it to a public server ( another thing I've never done ) as I have ideas about adding something similar to one of my paying apps - any recommendations on a host and how to go about it ? Once again thanks for your help
"We can't stop here - this is bat country" - Hunter S Thompson - RIP
|
|
|
|
|
I host most of my external apps on Linode.
You basically get a VM to do with as you please, the cost is about $20 per month for the lowest offering which is plenty powerful enough to run a few dotnet core apps with PostgreSQL as a DB and nginx as the front end proxy.
You get 3TB monthly allowance, and if you run over it's a pay as you use model at something like 1c for every 1000mb you go over...
I generally run Ubuntu on the ones I set up, what I tend to do is build a VM inside my own DC with the same specs as the linode, furnish said VM with Jenkins, dotnet core and nginx.
I then set Jenkins up to check my git server, and every time I check in, Jenkins builds and deploys the dotnet core app to the same VM, one I'm happy the built one's working I manually run the second job on jenkins which takes the deployed service pushes it to a separate GIT server, that the Linode VM is monitoring, when it sees a new build, it pulls that ready built to the linode, and deploys it.
With this process, I can basically check a change in on my desktop using visual studio, wait a couple of minutes, validate the build on staging, then click a button in a web page and it's live in about 5 mins.
I can go from VS to live usually in about 30 mins.
|
|
|
|