最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

C# Factory Method to Convert between similar types and preserve data - Stack Overflow

programmeradmin3浏览0评论

I'm trying to implement a factory method that attaches to two classes. They're essentially the same class, but belong to different namespaces. I want to preserve the data between the two classes as well. This is the outline of what I'm trying to do, but I'm very new to C#.

namespace MyMainNameSpace
{
   using MySubNameSpace;
   using MyOtherSubNameSpace;

   public static class FactoryClass
   {
      public static MyFirstClass ConvertToSecondClass()
      {
         var mySecondClass = new MySecondClass()
         {
            Attr1 = MyFirstClass().Attr1;
            Attr2 = MySecondClass().Attr2;
         };
         return mySecondClass;

      public static MySecondClass ConvertToFirstCLass()
      {
         var myFirstClass = new MyFirstClass()
         {
            Attr1 = MySecondClass().Attr1;
            Attr2 = MySecondClass().Attr2;
         }
         return myFirstClass;
      }
   }
}

namespace MyOtherSubNameSpace
{
   public class MySecondClass
   {
      public required string Attr1 { get; set; }
      public required string Attr2 { get; set; }
   }
}
namespace MySubNameSpace
{
   public class MyFirstClass
   {
      public required string Attr1 { get; set; }
      public required string Attr2 { get; set; }
   }
}

How do I extract the attributes Attr1 and Attr2 from each class to populate the new one in my factory method?

I'm trying to implement a factory method that attaches to two classes. They're essentially the same class, but belong to different namespaces. I want to preserve the data between the two classes as well. This is the outline of what I'm trying to do, but I'm very new to C#.

namespace MyMainNameSpace
{
   using MySubNameSpace;
   using MyOtherSubNameSpace;

   public static class FactoryClass
   {
      public static MyFirstClass ConvertToSecondClass()
      {
         var mySecondClass = new MySecondClass()
         {
            Attr1 = MyFirstClass().Attr1;
            Attr2 = MySecondClass().Attr2;
         };
         return mySecondClass;

      public static MySecondClass ConvertToFirstCLass()
      {
         var myFirstClass = new MyFirstClass()
         {
            Attr1 = MySecondClass().Attr1;
            Attr2 = MySecondClass().Attr2;
         }
         return myFirstClass;
      }
   }
}

namespace MyOtherSubNameSpace
{
   public class MySecondClass
   {
      public required string Attr1 { get; set; }
      public required string Attr2 { get; set; }
   }
}
namespace MySubNameSpace
{
   public class MyFirstClass
   {
      public required string Attr1 { get; set; }
      public required string Attr2 { get; set; }
   }
}

How do I extract the attributes Attr1 and Attr2 from each class to populate the new one in my factory method?

Share Improve this question asked Mar 5 at 19:26 AdamAdam 1981 silver badge11 bronze badges 4
  • 2 Your methods don't take the original data to be converted as parameters, therefore you can't know how to create the other one. – Alejandro Commented Mar 5 at 19:30
  • 3 An "interface" for Attr1 and Attr2 would help untangle things. – Gerry Schmitz Commented Mar 5 at 19:37
  • Can you please help me define an interface to untangle the Attr1 and Attr2 attributes? – Adam Commented Mar 5 at 19:45
  • 3 @Adam what's your goal of converting one type to the other? This question is very abstract so giving guidance is tough. If you just want to be able to use both types interchangeably, then use a common interface like Gerry suggested. – Devon Bessemer Commented Mar 5 at 20:55
Add a comment  | 

2 Answers 2

Reset to default 1

Your factory methods need to get an instance of the object they are trying to convert. You might try something like this:

public static MySecondClass ConvertToSecondClass(MyFirstClass mfc)
{
   var mySecondClass = new MySecondClass()
   {
      Attr1 = mfc.Attr1;
      Attr2 = mfc.Attr2;
   };
   return mySecondClass;
}

public static MyFirstClass ConvertToFirstCLass(MySecondClass msc)
{
   var myFirstClass = new MyFirstClass()
   {
      Attr1 = msc.Attr1;
      Attr2 = msc.Attr2;
   }
   return myFirstClass;
}

Then you can use them:

MyFirstClass a = new MyFirstClass()
{
    Attr1 = "Foo",
    Attr2 = "Bar"
};

MySecondClass b = ConvertToSecondClass(a);

Another nice way to do this is to use extension methods:

public static MySecondClass ConvertToSecondClass(this MyFirstClass mfc)
{
   var mySecondClass = new MySecondClass()
   {
      Attr1 = mfc.Attr1;
      Attr2 = mfc.Attr2;
   };
   return mySecondClass;
}

public static MyFirstClass ConvertToFirstCLass(this MySecondClass msc)
{
   var myFirstClass = new MyFirstClass()
   {
      Attr1 = msc.Attr1;
      Attr2 = msc.Attr2;
   }
   return myFirstClass;
}

Then you can use them:

MyFirstClass a = new MyFirstClass()
{
    Attr1 = "Foo",
    Attr2 = "Bar"
};

MySecondClass b = a.ConvertToSecondClass();
public interface IMyInterface
    {
        string Attr1 { get; }
        string Attr2 { get; }
    }

    public class MyFirstClass : IMyInterface
    {
        public string Attr1 { get; private set; }

        public string Attr2 { get; private set; }

        public MyFirstClass(string attr1, string attr2)
        {
            Attr1 = attr1;
            Attr2 = attr2;
        }

        public static implicit operator MyFirstClass(MySecondClass msc)
        {
            return new MyFirstClass(msc.Attr1, msc.Attr2);
        }
    }

    public class MySecondClass : IMyInterface
    {
        public string Attr1 { get; private set; }

        public string Attr2 { get; private set; }

        public MySecondClass(string attr1, string attr2)
        {
            Attr1 = attr1;
            Attr2 = attr2;
        }

        public static implicit operator MySecondClass(MyFirstClass mfc)
        {
            return new MySecondClass(mfc.Attr1, mfc.Attr2);
        }
    }

Then to call it we can use.

    MyFirstClass mfc = new MySecondClass("Attr1", "Attr2");
    Console.WriteLine(mfc.Attr1);

Or we could change the implicit operators to explicit operators and use this.

    MyFirstClass mfc = new MyFirstClass("Attr1", "Attr2");
    MySecondClass msc = (MySecondClass)mfc;
发布评论

评论列表(0)

  1. 暂无评论