views:

536

answers:

2

Hello.. Please I have a class in c# whose main function is to return the types and objects as dictionary Over a service .

Is it possible to cast the Object sents over the WCF service in the front end.

I.e using reflection to get the type of an object from the types.ToString() and using the type to cast the objects.

NB the Class that returns the dictionary and my frontend are in different projects so different Namespaces

Type repType = typeof(List <>).MakeGenericType(Type.GetType(EntityandTypes[entity])); object rep = Assembly.GetAssembly(repType).CreateInstance(repType.FullName); grdResult.ItemsSource = e.Result.ToList().Cast();

Note : EntityandTypes is a dictionary that contains Object and Their types .

+2  A: 

What would you want to do with the cast values? Casts usually make a difference at compile-time whereas you're asking for something at execution-time.

If you can explain how you'd want to use this, we can probably help you design around it.

Jon Skeet
I am actually return an entityObjects and Binding to a datagrid in silverlight.I would Like to find out the properties of this entityObjects and bind to it.Say for instance I Have a ressultList<EntityObject> en = e.Resulte.Result returns a list of entity objects but I need to Know which Entity Object They really are . so I need a method like thisDatagrid.itemSource = en.cast<getType(Nameofclass in string) >();
Why do you need to know which entity type they are? What benefit would that give you, bearing in mind that you don't know the type at compile-time?
Jon Skeet
+1  A: 

What sort of types are we talking about? classes? And what type of service is it?

If it is WCF, one option is to use type-sharing to use the same type at each end, but this abuses SOA a little bit. You can't cast a class to a very different type, but you can project into a different class. Various approaches for this are discussed here:

How to copy value from class X to class Y with the same property name in c#?

Marc Gravell