小编Mat*_*att的帖子

2个对象,完全相同(命名空间除外)c#

我正在使用第三方的网络服务,我遇到了一个小麻烦.在我手动创建一个方法将每个属性从源复制到目标之前,我想我会在这里要求更好的解决方案.

我有两个对象,一个是Customer.CustomerParty,另一个是Appointment.CustomerParty.CustomerParty对象实际上是属性和子对象完全相同.但是我无法从1投射到另一个.

所以,我需要从网络服务中找到某个人.我可以通过调用Customer.FindCustomer(customerID)来做到这一点,它返回一个Customer.CustomerParty对象.

我需要接受我找到的那个人,然后在"CreateAppointment"请求中使用它们几行.Appointment.CreateAppointment接受约会对象,约会对象包含CustomerParty对象.

但是,它想要的CustomerParty对象实际上是Appointment.CustomerParty.我有一个Customer.CustomerParty.

明白了吗?有什么建议?

c# casting namespaces

15
推荐指数
2
解决办法
1万
查看次数

泛型问题.我只是不了解如何制作通用方法

我有一些XML,存储在一个表中.我知道反序列化应该是什么对象,但我有很多类型的对象.

所以,我有这个代码,它可以工作,但它非常特定于1种类型的对象.我想让它通用以接受许多类型的对象之一.

Type _type = typeof(ReynoldsRCI.Services.rey_SvcApptView)

XmlSerializer ser = new XmlSerializer(_type);
object sck;

using (StringReader reader = new StringReader(_msg.RawText))
{
   using (XmlTextReader xreader = new XmlTextReader(reader))
   {
      sck = ser.Deserialize(xreader);
   }
}
Run Code Online (Sandbox Code Playgroud)

但是,我喜欢的是一个1行方法调用,我将它传递给对象和文本的类型,它将反序列化它并将其返回给调用者以供使用.但是对于泛型来说,我完全是个白痴,所以我所拥有的代码甚至都没有编译.

public T DoStuff<T>(T _type, string RawText)
{
   // Doesn't like this line.  Error "The best overloaded method match for 'System.Xml.Serialization.XmlSerializer.XmlSerializer(System.Type)' has some invalid arguments"
   XmlSerializer ser = new XmlSerializer(_type);
   object retObj;

   using (StringReader reader = new StringReader(RawText))
   {
      using (XmlTextReader xreader = new XmlTextReader(reader))
      {
         retObj = …
Run Code Online (Sandbox Code Playgroud)

c# generics

1
推荐指数
1
解决办法
457
查看次数

标签 统计

c# ×2

casting ×1

generics ×1

namespaces ×1