I have 2 enums in 2 different objects. I want to set the enum in object #1 equal to the enum in object #2.
Here are my objects:
namespace MVC1 {
public enum MyEnum {
firstName,
lastName
}
public class Obj1{
public MyEnum enum1;
}
}
namespace MVC2 {
public enum MyEnum {
firstName,
lastName
}
public class Obj2{
public MyEnum enum1;
}
}
I want to do this, but this wont compile:
MVC1.Obj1 obj1 = new MVC1.Obj1();
MVC2.Obj2 obj2 = new MVC2.Obj2();
obj1.enum1 = obj2.enum1; //I know this won't work.
How do I set the enum in Obj1 equal to the enum in Obj2? Thanks
Assuming that you keep them the same, you can cast to/from int:
obj1.enum1 = (MVC1.MyEnum)((int)obj2.enum1);
Enums have an underlying integer type, which is int (System.Int32) by default, but you can explicitly specify it too, by using "enum MyEnum : type".
Because you're working in two different namespaces, the Enum types are essentially different, but because their underlying type is the same, you can just cast them:
obj1.enum1 = (MVC1.MyEnum) obj2.enum1;
A note: In C# you have to use parentheses for function calls, even when there aren't any parameters. You should add them to the constructor calls.
Best way to do it is check if it's in range using Enum.IsDefined:
int one = (int)obj2.enum1;
if (Enum.IsDefined(typeof(MVC1.MyEnum), one )) {
obj1.enum1 = (MVC1.MyEnum)one;
}
obj1.enum1 = (MVC1.MyEnum) Enum.Parse(typeof(MVC1.MyEnum),
((int)obj2.enum1).ToString());
or
int one = (int)obj2.enum1;
obj1.enum1 = (MVC1.MyEnum)one;
Related
In C# or VB.NET, under WinForms, I have a property that returns an array of a enum. See an example:
public enum TestEnum: int {
Name1 = 0,
Name2 = 1,
Name3 = 2
} // Note that the enum does not apply for the [Flags] attribute.
public TestEnum[] TestProperty {get; set;} =
new[] {TestEnum.Name1, TestEnum.Name2, TestEnum.Name3};
By default, a PropertyGrid will show the values as int[], like: {0, 1, 2} instead of the enumeration value names, like: {"Name1", "Name2", "Name2"}, which is the visual representation that I would like to acchieve...
So, I would like to design a TypeConverter that could be able to return a string array with the value names, and apply it like this:
[TypeConverter(typeof(EnumArrayToStringArrayTypeConverter))]
public TestEnum[] TestProperty {get; set;} =
new[] {TestEnum.Name1, TestEnum.Name2, TestEnum.Name3};
In other words, If my property is represented like this in a PropertyGrid:
I would like to have this:
The biggest problem I'm facing is trying to retrieve the type of the enum from the custom type-converter class, to be able get the value names of that enum. I only can get the primitive data type of the array (like: int[], uint16[], etc)...
public class EnumArrayToStringArrayTypeConverter : TypeConverter {
// ...
public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture,
object value,
Type destinationType) {
if (destinationType == null) {
throw new ArgumentNullException(nameof(destinationType));
}
try {
// This will return the array-type for the
// primitive data type of the declared enum,
// such as int[], uint16[], etc.
Type t = value.GetType();
// I'm stuck at this point.
// ...
} catch (Exception ex) {
}
return null;
}
// ...
}
Please take into account that I'm asking for a reusable solution that can work for any kind of enum. And, my enum in this example does not have the [Flags] attribute applied, but a solution should care about enums having it, so, If a enum item of the enum array is a enum that has various flags, those flags (the value names) should be concatenated for example using string.join().
The PropertyGrid does already show the names for the enum values. It can even handle [Flags] correctly. See the sample below using a form with a default PropertyGrid and a default button and nothing else.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[Flags]
public enum TestEnum : int
{
Name1 = 0,
Name2 = 1,
Name3 = 2
}
public class TestObject
{
public string Name { get; set; } = "Hello World";
public TestEnum[] TestProperty { get; set; } =
new[] { TestEnum.Name1, TestEnum.Name2 | TestEnum.Name3, TestEnum.Name3 };
}
private void button1_Click(object sender, EventArgs e)
{
TestObject o = new TestObject();
propertyGrid1.SelectedObject = o;
}
}
Please supply some example code that can reproduce that the enum names are not shown in the PropertyGrid. You must be doing something wrong in the first place.
As mentioned by #NineBerry in his answer, the PropertyGrid does already show the names for the enum values. However, I discovered that exists a strange circumstance on wich it will not do it...
Since my original source-code is written in VB.NET, I'll put VB.NET sample codes to reproduce the issue.
The thing is that I was getting a value from a instance of a WMI class (specifically: Win32_DiskDrive.Capabilities), which returns an object that needs to be casted to a uint16 array. Then, I was casting that resulting uint16 array to my type of enum. To simplify things, I will not show WMI code, but an object that represents what I was getting from WMI...
Dim wmiValue As Object = {1US, 2US, 3US}
Dim castedValue As UShort() = DirectCast(wmiValue, UShort())
TestProperty = DirectCast(castedValue, TestEnum())
So, when doing that type-casting, and thanks to #NineBerry answer, I discovered that for some reason the default type converter of TestProperty goes wrong and the PropertyGrid shows uint16 values instead of the enum value-names.
( Note that using DirectCast() or CType() in VB.NET, it didn't changed the PropertyGrid behavior. )
To fix the error, I ended using Array.ConvertAll(), and then the PropertyGrid properly shows the value-names...
Dim wmiValue As Object = {1US, 2US, 3US}
Dim castedValue As UShort() = DirectCast(wmiValue, UShort())
TestProperty = Array.ConvertAll(castedValue,
Function(value As UShort)
Return DirectCast(value, TestEnum)
End Function)
I'm trying to use Automapper to map from a regular enum to an Enumeration Class (as described by Jimmy Bogard - http://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/). The regular enum doesn't have the same values as the enumeration class does. I would therefore like to map using the Name if possible:
Enum:
public enum ProductType
{
ProductType1,
ProductType2
}
Enumeration Class:
public class ProductType : Enumeration
{
public static ProductType ProductType1 = new ProductType(8, "Product Type 1");
public static ProductType ProductType2 = new ProductType(72, "Product Type 2");
public ProductType(int value, string displayName)
: base(value, displayName)
{
}
public ProductType()
{
}
}
Any help to make this mapping work appreciated! I have attempted just a regular mapping:
Mapper.Map<ProductType, Domain.ProductType>();
.. but the mapped type has a value of 0.
Thanks,
Alex
Here is how Automapper works - it gets public instance properties/fields of destination type, and matches the with public instance properties/fields of source type. Your enum does not have public properties. Enumeration class has two - Value and DisplayName. There is nothing to map for Automapper. Best thing you can use is simple mapper function (I like to use extension methods for that):
public static Domain.ProductType ToDomainProductType(
this ProductType productType)
{
switch (productType)
{
case ProductType.ProductType1:
return Domain.ProductType.ProductType1;
case ProductType.ProductType2:
return Domain.ProductType.ProductType2;
default:
throw new ArgumentException();
}
}
Usage:
ProductType productType = ProductType.ProductType1;
var result = productType.ToDomainProductType();
If you really want to use Automapper in this case, you ca provide this creation method to ConstructUsing method of mapping expression:
Mapper.CreateMap<ProductType, Domain.ProductType>()
.ConstructUsing(Extensions.ToDomainProductType);
You also can move this creation method to Domain.ProductType class. Then creating its instance from given enum value will look like:
var result = Domain.ProductType.Create(productType);
UPDATE: You can use reflection to create generic method which maps between enums and appropriate enumeration class:
public static TEnumeration ToEnumeration<TEnum, TEnumeration>(this TEnum value)
{
string name = Enum.GetName(typeof(TEnum), value);
var field = typeof(TEnumeration).GetField(name);
return (TEnumeration)field.GetValue(null);
}
Usage:
var result = productType.ToEnumeration<ProductType, Domain.ProductType>();
I recently encountered a case when I needed to get an Enum object by value (to be saved via EF CodeFirst), and here is my Enum:
public enum ShipmentStatus {
New = 0,
Shipped = 1,
Canceled = 2
}
So I needed to get ShipmentStatus.Shipped object by value 1.
So how could I accomplish that?
This should work, either (just casting the int value to enum type):
int _val = 1;
ShipmentStatus _item = (ShipmentStatus)_val;
Beware, that it may cause an error if that enum is not defined.
Why not use this build in feature?
ShipmentStatus shipped = (ShipmentStatus)System.Enum.GetValues(typeof(ShipmentStatus)).GetValue(1);
After some battling with Enum I created this - a universal helper class that will do what I needed - getting key by value, and more importantly - from ANY Enum type:
public static class EnumHelpers {
public static T GetEnumObjectByValue<T>(int valueId) {
return (T) Enum.ToObject(typeof (T), valueId);
}
}
So, to get Enum object ShipmentStatus.Shipped this will return this object:
var enumObject = EnumHelpers.GetEnumObjectByValue<ShipmentStatus>(1);
So basicaly you can use any Enum object and get its key by value:
var enumObject = EnumHelpers.GetEnumObjectByValue<YOUR_ENUM_TYPE>(VALUE);
I have a class that used to have a string return type. Now I find I need to return more than a string. I was thinking to return something like below:
public string Test()
{
return ( new { ID = 5, Name= "Dave" } );
}
Is this even possible and if so then what would be the return type? I know it's not string ..
As others have said, the best thing to do here is to make a nominal type. I would suggest that the nominal type have the same characteristics as an anonymous type; that is, you should consider making the type immutable and consider making it exhibit value equality.
It is possible to return an anonymous type as object and then use the instance returned elsewhere using a variety of sneaky techniques. You can cast the object to "dynamic" (in C# 4) and then use the properties of the anonymous type, but this is slow and lacks compile-time type checking.
You can also use the "cast by example" trick, which does get you compile-time type checking. However, that trick only works when the anonymous source object and the anonymous example object come from the same assembly.
static T CastByExample<T>(object source, T example) where T : class
{
return source as T;
}
static object ReturnsAnonymous() { return new { X = 123 }; }
static void DoIt()
{
object obj = ReturnsAnonymous();
var example = new { X = 0 };
var anon = CastByExample(obj, example);
Console.WriteLine(anon.X); // 123
}
See how sneaky that is? We use method type inference and local variable type inference to tell the compiler "these two things are the same type". This lets you export an anonymous type as object and cast it back to anonymous type.
But you probably should not do this; if you're resorting to such sneaky tricks then you should simply be defining a nominal type in the first place. Also, like I said, the trick only works if the example and the source objects were created in code in the same assembly; two "identical" anonymous types in two different assemblies do not unify to be the same type.
The object that you return does have a class, but it's anonymous so you can't specify it in the code. You just have to return it as an object reference:
public object Test() {
return new { ID = 5, Name= "Dave" };
}
Note that the anonymous type is unknown outside the scope of the method, so reflection is the only way to access its properties.
If you want to be able to use the returned object conveniently, you should declare a class:
public class TestResult
{
public int ID { get; set; }
public string Name { get; set; }
}
public TestResult Test() {
return new TestResult() { ID = 5, Name= "Dave" };
}
Another alternative is to use an existing class, if it fits your purpose. A KeyValuePair is close to what you use, but then the properties will of course be named Key and Value instead of ID and Name:
public KeyValuePair<int, string> Test() {
return new KeyValuePair<int, string>(5, "Dave");
}
This isn't possible as the anonymous class is only valid within the current context. If you need to return an object then you'll need to create a real class.
I'm assuming you left string as the return type by accident.
Anonymous type are class type that are derived directly from object.
You can return it from method as object as return type.
Have a look at this.
No, it's not possible. Your options are:
Define a real class for the return value,
Use System.Tuple, or
Use out parameters (probably the least good option).
You can make a struct (or class) for this.
public struct IdAndName
{
public int Id;
public string Name;
public IdAndName(int id, string name)
{
ID = id;
Name = name;
}
}
You could also use a Tuple<T1, T2>, (but that's not recommended as the properties aren't named.
class NewString
{
public int ID { get; set; }
public string Name { get; set; }
}
public NewString Test()
{
return ( new NewString() { ID = 5, Name = "Dave" } );
}
:)
Consider this:
var me = new { FirstName = "John", LastName = "Smith" };
This is fine as we can then do this:
Console.WriteLine("{0} {1}", me.FirstName, me.LastName);
However we can't do this:
public T GetMe()
{
return new { FirstName = "John", LastName = "Smith" };
}
because we don't know the type of T.
We could do this:
public object GetMe()
{
return new { FirstName = "John", LastName = "Smith" };
}
but then we'd have to inspect the properties of the object using reflection in order to access them:
var p = new Prog();
object o = p.GetMe();
Type t = o.GetType();
foreach (var prop in t.GetProperties())
{
Console.WriteLine(prop.Name + ": " + prop.GetValue(o, null));
}
However what about if we could name an anonymous type as we define it? Of course it would no longer be anonymous, however it would be more succinct and maintainable than a normal class definition.
Consider this:
public Person GetMe()
{
return new public class Person { FirstName = "John", LastName = "Smith" };
}
The benefit being it would then be possible to return the result of a complicated Linq query from a method without having to define the class explicitly.
Consider this relatively complex Linq query:
List<int> list = new List<int>();
var query = from number in list
select
new
{
Number = number,
Square = number*number,
Absolute = Math.Abs(number),
Range = Enumerable.Range(0, number)
};
Instead of defining a class like so:
public class MyNumbers
{
public int Number { get; set; }
public int Square { get; set; }
public int Absolute { get; set; }
public IEnumerable<int> Range { get; set; }
}
in order to return the query variable from a method we could instead just do this:
List<int> list = new List<int>();
return from number in list
select new public class MyNumbers
{
Number = number,
Square = number*number,
Absolute = Math.Abs(number),
Range = Enumerable.Range(0, number)
};
Actually, there's a "hack" that you can do to get an anonymous type back from a method. Consider this:
public object MyMethod()
{
var myNewObject = new
{
stringProperty = "Hello, World!",
intProperty = 1337,
boolProperty = false
};
return myNewObject;
}
public T Cast<T>(object obj, T type)
{
return (T)obj;
}
You can now do this:
var obj = MyMethod();
var myNewObj = Cast(obj, new { stringProperty = "", intProperty = 0, boolProperty = false });
The myNewObj will now be an object of the same Type as the anonymous type.
The language feature you need is:
public var GetMe()
{
return new { FirstName = "John", LastName = "Smith" };
}
That is, var would be valid as a method return type, and the compiler would infer the actual type from whatever is returned. You would then have to do this at the call site:
var me = GetMe();
Any two anonymous types with members of the same type would be the same type, so if you wrote other functions returning the same pattern, they would have the same type. For any types A and B where B has a subset of the members of A, then A is assignment-compatible with B (B is like a base class of A). If you wrote:
public var GetMeFrom(var names)
{
return new { FirstName = names["First"], LastName = names["Last"] };
}
The compiler would effectively define this as a generic method with two type parameters, T1 being the type of names and T2 being the type returned by the indexer on T1 that accepts a string. T1 would be constrained so that it must have an indexer that accepts a string. And at the call site you would just pass anything that had an indexer that accepted a string and returned any type you like, and that would determine the type of FirstName and LastName in the type returned by GetMeFrom.
So type inference would figure all this out for you, automatically capturing whatever type constraints are discoverable from the code.
IMHO the root problem is nothing to do with anonymous types, but that declaring a class is too verbose.
Option 1:
If you could declare a class like this:
public class MyClass
{ properties={ int Number, int Square, int Absolute, IEnumerable<int> Range } }
or some other similarly quick way (like the tuple example) then you wouldn't feel the need to do hacky things with anonymous types just to save some code.
When 'compiler as a service' arrives in C#5, hopefully they'll do a good job of integrating it and we'll be able to use metaprogramming to solve these kinds of problems cleanly. Party like it's 1958!
Option 2:
Alternatively, in C#4, you could just pass an anonymous type around as dynamic and avoid all the casting. Of course this opens you up to runtime errors if you rename a variable, etc.
Option 3:
If C# would implement generics in the same way as C++, then you could pass the anonymous type into a method, and so long as it had the right members, it would just compile. You'd get all the benefits of static type safety, and none of the downsides. Every time I have to type where T : ISomething in C# I get annoyed that they didn't do this!
What you are describing (named anonymous types) are basically "tuple types".
I think they would be a nice addition to C#.
If I were designing such a feature for C#, I would expose it using syntax like this:
tuple<int x, int y>
so that you could do:
public tuple<int x, int y> GetStuff()
{
}
I would then change the definition of anonymous types, so that:
new { x = 2, y = 2}
had tuple<int x, int y> as it's type, rather than an anonymous type.
Getting this to work with the current CLR is a little tricky, because once you can name an anonymous type in public signatures you need to be able to unify them across separately compiled assemblies. It can be accomplished by embedding a "module constructor" inside any assembly that uses a tuple type. See this post for an example.
The only downside to that approach is that it doesn't respect the CLR's "lazy" model for type generation. That means that assemblies that use many distinct tuple types might experience slightly slower load types. A better approach would be to add support for tuple types directly to the CLR.
But, apart from changing the CLR, I think the module constructor approach is the best way of doing something like this.
I would love this feature, there have been many times I've wanted this.
A good example is processing XML. You parse them get back an object, but then you need to make a concrete version of the object to send back to a caller. Many times you get XML that changes quite considerably and requires you make many classes to handle it. Wouldn't it be wonderful if you could just build the object using LinqToXml as a var, then just return that?
I think this would be a nice compiler magic for tuples:
Creating a tuple:
(int, string, Person) tuple = (8, "hello", new Person());
equivalent to:
Tuple<int,string,Person> tuple = new Tuple<int,string,Person>(8 ,"hello", new Person());
In a function:
public (int, string, Person) GetTuple(){
return ...
}
Getting values:
int number = tuple[1];
string text = tuple[2];
Person person = tuple[3];
Could you create an Interface with the properties FirstName and LastName and use that?