在C#中动态生成Cast类型 [英] Generate Cast type dynamically in C#

查看:638
本文介绍了在C#中动态生成Cast类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在C#中有一个类,其主要功能是将类型和对象作为字典返回服务。



是否可以将对象转换为在前端的WCF服务。



使用反射从types.ToString()获取对象的类型,并使用类型来投射对象。 / p>

注意返回字典的类,我的前端在不同的项目中,因此不同的命名空间:

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

注意:EntityandTypes是一个包含Object及其类型的字典。

解决方案

你想用cast值来做什么?演员通常会在编译时有所不同,而您正在执行执行时间



如果你可以解释你想如何使用它,我们可以帮你设计一下。


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<typeof(Type.GetType(EntityandTypes[entity]))>();

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

解决方案

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.

这篇关于在C#中动态生成Cast类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆