从变量或类型对象重新构造对象。 [英] Recasting object from a variable or type object.

查看:123
本文介绍了从变量或类型对象重新构造对象。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义类,已经分配给一个变量并且使用过,我现在需要将这个类作为一个不同的类来构建,这个类是单独的

汇编,目前不在项目中。通常,如果它是

项目的一部分,我会说:


MyNewClass newClassObject =(MyNewClass)currentClassObject;


我在单独的变量中有类,汇编和命名空间的名称。

我甚至有一个变量??? Typea?那等于一个?? MyNewClassa ?? (从下面

代码)


string stuffToLoad = myNameSpace +"。" + myClass +"," + myAssembly;

类型myType = Type.GetType(stuffToLoad,true);


我的问题是我需要将currentClassObject重新转换为MyNewClass并且

我似乎已经没有选择了。已经尝试了几条道路。任何人

有什么想法吗?


-phil

解决方案

< blockquote>我只是不明白,如果你有一个对程序集的引用,你

应该能够做一些与案例有效相同的事情:

MyNewClass newClassObject =(MyNewClass)currentClassObject;


也许我想念你的观点。


Jianwei


Phil写道:我有个定制已经分配给变量并使用过的类,我现在需要将这个类强制转换为另一个类,它在一个单独的
程序集中,当前不在项目中。通常,如果它是
项目的一部分我会说:

MyNewClass newClassObject =(MyNewClass)currentClassObject;

我有类,汇编和名称命名空间在单独的变量中。
我甚至有一个变量的?? Typea?那等于一个?? MyNewClassa ?? (来自下面
代码)

string stuffToLoad = myNameSpace +"。 + myClass +"," + myAssembly;
输入myType = Type.GetType(stuffToLoad,true);

我的问题是我需要将currentClassObject重新转换为MyNewClass并且
我似乎已经用完了选项。已经尝试了几条道路。任何人都有什么想法?

-phil



" Jianwei Sun"写道:

我只是不明白,如果你有一个参考集会,你应该能够像案件有效一样做一些事情:




也许这是我的问题,它太简单了< w>。我只是在C#工作了大约六个月,所以对我来说还是新手。


//设置汇编文件名

string assemblyFileName = String.Concat(GetAppPath(),myAssembly," .dll");

//创建程序集

程序集a = Assembly.LoadFrom (assemblyFileName);


MyNewClass newClassObject =(MyNewClass)currentClassObject;


---


使用上面对正确装配的参考我如何重新制作?

我试过:


typeof(a.gettype(myClass)newClassObject =( typeof(a.gettype(myClass))

currentClassObject;


和其他几个变种?|


Phil< Ph ** @ discuss.microsoft.com>写道:

我有一个自定义类,已经分配给一个变量并与之合作,我
现在需要将这个类作为一个不同的类进行转换,这是一个单独的
程序集,目前没有参与该项目。通常,如果它是
项目的一部分我会说:

MyNewClass newClassObject =(MyNewClass)currentClassObject;

我有类,汇编和名称命名空间在单独的变量中。
我甚至有一个?类型的变量?那等于?MyNewClass? (来自下面
代码)

string stuffToLoad = myNameSpace +"。 + myClass +"," + myAssembly;
输入myType = Type.GetType(stuffToLoad,true);

我的问题是我需要将currentClassObject重新转换为MyNewClass并且
我似乎已经用完了选项。已经尝试了几条道路。任何人有什么想法吗?




嗯,你为什么要演员?大概是使用

类型的成员 - 如果你在编译时知道该成员,你怎么不知道

实际类型?这里通常的答案是你知道类型实现的一些接口

,你只需要在那个

接口中调用一个成员 - 在这种情况下,只需要转换为接口。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


I have a custom class that has been assigned to a variable and worked with, I
now need to cast this class as a different class, which is in a separate
assembly, not currently in the project. Normally if it was part of the
project I would say:

MyNewClass newClassObject = (MyNewClass) currentClassObject;

I have the name of the class, assembly and namespace in separate variables.
I even have a variable of a??Typea?? that is equal to a??MyNewClassa?? (from below
code)

string stuffToLoad = myNameSpace + "." + myClass + ", " + myAssembly;
Type myType = Type.GetType(stuffToLoad, true);

My problem is that I need to recast currentClassObject into MyNewClass and
I seem to have run out of options. Have tried several roads already. Anyone
have any ideas?

-phil

解决方案

I just don''t understand, if you have a reference to the assembly, you
should be able to do something the same as if the case is valid :

MyNewClass newClassObject = (MyNewClass) currentClassObject;
Maybe I miss your point.

Jianwei

Phil wrote: I have a custom class that has been assigned to a variable and worked with, I
now need to cast this class as a different class, which is in a separate
assembly, not currently in the project. Normally if it was part of the
project I would say:

MyNewClass newClassObject = (MyNewClass) currentClassObject;

I have the name of the class, assembly and namespace in separate variables.
I even have a variable of a??Typea?? that is equal to a??MyNewClassa?? (from below
code)

string stuffToLoad = myNameSpace + "." + myClass + ", " + myAssembly;
Type myType = Type.GetType(stuffToLoad, true);

My problem is that I need to recast currentClassObject into MyNewClass and
I seem to have run out of options. Have tried several roads already. Anyone
have any ideas?

-phil



"Jianwei Sun" wrote:

I just don''t understand, if you have a reference to the assembly, you
should be able to do something the same as if the case is valid :



Perhaps that is my problem, that it is too simple <w>. Ia??ve only been
working in C# for about six months so it is still new to me.

// Set Assembly Filename
string assemblyFileName = String.Concat(GetAppPath(), myAssembly, ".dll");
// Create Assembly
Assembly a = Assembly.LoadFrom(assemblyFileName);

MyNewClass newClassObject = (MyNewClass) currentClassObject;

---

Using the above reference to the proper assembly how do I recast as above?
I tried:

typeof(a.gettype(myClass) newClassObject = (typeof(a.gettype(myClass))
currentClassObject;

and several other variationsa?|


Phil <Ph**@discussions.microsoft.com> wrote:

I have a custom class that has been assigned to a variable and worked with, I
now need to cast this class as a different class, which is in a separate
assembly, not currently in the project. Normally if it was part of the
project I would say:

MyNewClass newClassObject = (MyNewClass) currentClassObject;

I have the name of the class, assembly and namespace in separate variables.
I even have a variable of ?Type? that is equal to ?MyNewClass? (from below
code)

string stuffToLoad = myNameSpace + "." + myClass + ", " + myAssembly;
Type myType = Type.GetType(stuffToLoad, true);

My problem is that I need to recast currentClassObject into MyNewClass and
I seem to have run out of options. Have tried several roads already. Anyone
have any ideas?



Well, why do you want to cast? Presumably it''s to use a member of the
type - if you know the member at compile time, how come you don''t know
the actual type? The usual answer here is that you know some interface
that the type implements, and you only need to call a member in that
interface - in which case, just cast to the interface.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于从变量或类型对象重新构造对象。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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