C #: Dynamic casting types

I am currently using this type of code:

private void FillObject(Object MainObject, Foo Arg1, Bar Arg2) { if (MainObject is SomeClassType1) { SomeClassType1 HelpObject = (SomeClassType1)MainObject; HelpObject.Property1 = Arg1; HelpObject.Property2 = Arg2; } else if (MainObject is SomeClassType2) { SomeClassType2 HelpObject = (SomeClassType2)MainObject; HelpObject.Property1 = Arg1; HelpObject.Property2 = Arg2; } } 

Assuming SomeClassType1 and SomeClassType2 have the same set of properties that I want to assign (although they may differ in others), is it possible to dynamically distinguish MainObject to the appropriate type and then assign the value without duplicating the code? Here is what I would like to see at the end:

 private void FillObject(Object MainObject, Foo Arg1, Bar Arg2) { Type DynamicType = null; if (MainObject is SomeClassType1) { DynamicType = typeof(SomeClassType1); } else if (MainObject is SomeClassType2) { DynamicType = typeof(SomeClassType2); } DynamicType HelpObject = (DynamicType)MainObject; HelpObject.Property1 = Arg1; HelpObject.Property2 = Arg2; } 

And, obviously, C # complains about the inability to find DynamicType:

Unable to find the name of the type or namespace DynamicType (are you missing the using directive or assembly references?)

Is this possible in C # 2.0? If this is messier than my current code, than I don't see the point in this, but I'm very interested to know. Thanks!

EDIT: just for clarification, I understand very well that implementing an interface is the most appropriate and probably the right solution. However, I'm more interested in how I could do this without implementing an interface. Thanks for the great answers!

+6
c # dynamic-cast
source share
7 answers

Both types that you need seem to implement the same two properties. In this case, you must define an interface for these properties:

 public interface IMyInterface { public Foo Property1 {get; set;} public Bar Property2 {get;set;} } 

Then make sure that each of your classes tells the compiler that they are implementing this new interface. Finally, use a generic method with a type argument that is limited to this interaction:

 private void FillObject<T>(T MainObject, Foo Arg1, Bar Arg2) where T : IMyInterface { MainObject.Property1 = Arg1; MainObject.Property2 = Arg2; } 

Please note that even with additional code to declare the interface, these fragments still end up shorter than one of the fragments you submitted in the question, and this code is much easier to expand if the number of types you care about increases.

+15
source share

Essentially, you are writing a switch statement based on an object type here. There is essentially no good way to do this otherwise than your first example (which is tedious at best).

I wrote a small structure for switching types, which makes the syntax a bit more concise. It allows you to write code as follows.

 TypeSwitch.Do( sender, TypeSwitch.Case<Button>( () => textBox1.Text = "Hit a Button"), TypeSwitch.Case<CheckBox>( x => textBox1.Text = "Checkbox is " + x.Checked), TypeSwitch.Default( () => textBox1.Text = "Not sure what is hovered over")); 

Blog post: http://blogs.msdn.com/jaredpar/archive/2008/05/16/switching-on-types.aspx

+9
source share

Can you make changes to SomeClassType1, SomeClassType2, etc.? If so, then I suggest you create an interface containing your common properties, and then proceed with this interface in FillObject() to set the properties.

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SO566510 { interface IMyProperties { int Property1 { get; set; } int Property2 { get; set; } } class SomeClassType1 : IMyProperties { public int Property1 { get; set; } public int Property2 { get; set; } } class SomeClassType2 : IMyProperties { public int Property1 { get; set; } public int Property2 { get; set; } } class Program { static void Main(string[] args) { var obj1 = new SomeClassType1(); var obj2 = new SomeClassType2(); FillObject(obj1, 10, 20); FillObject(obj2, 30, 40); } private static void FillObject(IMyProperties objWithMyProperties , int arg1, int arg2) { objWithMyProperties.Property1 = arg1; objWithMyProperties.Property2 = arg2; } } } 
+7
source share
 private void FillObject(Object MainObject, Foo Arg1, Bar Arg2) { Type t = MainObject.GetType(); t.GetProperty("Property1").SetValue(MainObject, Arg1, null); t.GetProperty("Property2").SetValue(MainObject, Arg2, null); } 
+4
source share

Some ideas:

+3
source share

Instead of trying to use, perhaps you can try to set properties using reflection.

+2
source share

I am dynamically throwing objects using Reflection in an ASP.NET MVC application. Basically, I list the properties of the class, find the corresponding value in the data warehouse and dynamically drop the value and assign it to the object instance. See my blog Dynamic Casting with .NET

0
source share

All Articles