[英]“Shapeless” objects in c#
I am receiving objects using wcf, each object is different and I would like to instead of creating an object for each received object so I can manipulate it, to pass/copy each received object to only one shapeless object each time. 我正在使用wcf接收对象,每个对象都是不同的,并且我不想为每个接收到的对象创建一个对象,以便可以对其进行操作,以便每次将每个接收到的对象传递/复制到一个不变形的对象上。 It there such a thing?
有这样的事吗?
A pseudo-code example: 伪代码示例:
Shapeless_object = received_obj_A;
if (Shapeless_object.name = "I_dont_know") {
Shapeless_object.count++;
}
Shapeless_object = received_obj_B;
if (Shapeless_object.name = "I_dont_know_too") {
Shapeless_object.count--;
Shapeless_object.age = 20;
}
received_obj_A and B are different with different parameters but passed in a single object. receive_obj_A和B的参数不同,但传递给单个对象。 I hope I made my question as clear as possible.
我希望我的问题尽可能清楚。
You mention WCF origins: it is easy to add interfaces to WCF types via partial class
: just declare the common interface - perhaps: 您提到了WCF的起源:通过
partial class
将接口添加到WCF类型很容易:只需声明公共接口-也许是:
interface ICommon {
string name {get;}
// ...
}
Then tell the compiler to infer the interface for each of your WCF types: 然后告诉编译器为每种WCF类型推断接口:
namespace TheWcfNamespace {
partial class SomeWcfType : ICommon {}
partial class AnotherWcfType : ICommon {}
partial class WhateverWcfType : ICommon {}
}
As long as SomeWcfType
etc all have the members to implement the interface, you can now treat all your WCF objects as ICommon
. 只要
SomeWcfType
等都具有实现接口的成员,您现在就可以将所有WCF对象都视为ICommon
。
Alternatively, you might be able to do what you want here via dynamic
- just replace Shapeless_object
with dynamic
- however, that seems an abuse of the intent. 另外,您可能可以通过
dynamic
方式执行您想要的操作-只需将Shapeless_object
替换为dynamic
-但是,这似乎滥用了此意图。 A more classic implementation here would be interfaces: 这里更经典的实现是接口:
if(obj is IDontKnow) {
// TODO: access as an IDontKnow
}
if (obj is IDontKnowToo) {
// TODO: access as an IDontKnowToo
}
You can of course combine this with the partial class
approach mentioned above. 您当然可以将其与上面提到的
partial class
方法结合起来。
You could cast them to dynamic
, but it would be a performance hit. 您可以将它们转换为
dynamic
,但这会带来性能上的损失。
dynamic shapeless = received_obj_A;
if (shapeless .name = "I_dont_know") {
shapeless.count++;
}
shapeless = received_obj_B;
if (shapeless.name = "I_dont_know_too") {
shapeless.count--;
shapeless.age = 20;
}
Note that you do lose static typing, obviously. 请注意,显然您会丢失静态类型。
Do you mean this? 你是这个意思吗
var a = received_obj_A;
if (a.name = "I_dont_know") {
Shapeless_object.count++;
}
var b = received_obj_B;
if (b.name = "I_dont_know_too") {
Shapeless_object.count--;
Shapeless_object.age = 20;
}
Dont use the base object here, because you need to check for the instance and try to cast it. 不要在此处使用基础对象,因为您需要检查实例并尝试对其进行强制转换。
Don't do: 不要做:
object a = received_obj_A; // Here you need to cast
if (a.name = "I_dont_know") {
Shapeless_object.count++;
}
object b = received_obj_B; // Here you need to cast
if (b.name = "I_dont_know_too") {
Shapeless_object.count--;
Shapeless_object.age = 20;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.