在运行时设置DataType以声明变量 [英] Set DataType at runtime for already declare variable

查看:56
本文介绍了在运行时设置DataType以声明变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我在类中声明了变量,即
public static Logic1 MyVar;

现在我想在运行时将变量数据类型从"Logic1"更改为;改为"Logic2",这样"MyVar"将用于访问Logic2中某些零件代码的变量.

我需要此权限,因为我对用户选择的逻辑类型进行了设置.

在运行时更改dataType必须是永久的,因此,每次"Myvar"浏览器都不需要类型转换.


感谢您的帮助!

解决方案

在C#中是不可能的!!!!! !!!!

但是,解决方法仍然存在.

声明一个接口ILogic,并放置在Logic1&之间共享的所有方法/成员. Logic2类.同时具有Logic1& Logic2实现此接口ILogic.

然后创建一个ILogic myVar类型的变量;现在,如果说myVar = new Logic1();那么myVar将引用Logic1.一段时间后,如果您说myVar =某个Logic2实例-那么它将引用Logic2.

希望能回答您的问题.

谢谢,
Viral.

Hello,

I m having variable declared in class
i.e
public static Logic1 MyVar;

now at the runtime i want to change variable datatype  from "Logic1" to "Logic2"
so "MyVar" will be used for accessing variables in Logic2 for some Part code.

This wat i need because i ve a setting on type of logic selected by user.

Changing dataType at runtime must be permenent, so no type casting is required every time the "Myvar" object is accesssed for use.


Thanks for the help!

解决方案

That is not possible in C#!!!!!!!!!!!!

However, a work around is there.

Declare an interface ILogic and put all the methods/members that are shared between Logic1 & Logic2 classes. Have both the classes Logic1 & Logic2 implement this interface ILogic.

Then create a variable of type ILogic myVar;

Now, if you say myVar = new Logic1(); then myVar would refer to Logic1. After some time if you say myVar = some Logic2 instance - then it would refer to Logic2.

Hope that answers your question.

Thanks,
Viral.


这篇关于在运行时设置DataType以声明变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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