2

I know we can only create instances of classes but I'd like to set an interface for my method and just creating an instance within my Deserialize() method:

 public T Deserialize(string contents)
        {
            Type type = typeof(T);

            var obj = Activator.CreateInstance(type);

I'd like to use it this way:

var customSerializer = new CustomSerializer<IPerson>();
IPerson person = customSerializer.Deserialize(contents);

It doesn't matter what object it creates, it just have to have the basic properties that my T interface provides.

How could I achieve this?

Thanks,

Charles
  • 50,943
  • 13
  • 104
  • 142
The Light
  • 26,341
  • 62
  • 176
  • 258
  • 2
    You have to tell it what type of class to create, and it can't be an interface. End of story. – Jonathon Reinhart Dec 09 '12 at 20:41
  • that's very bad, I'd need to work with interfaces rather than concrete classes. – The Light Dec 09 '12 at 20:45
  • Make some `Person` class that implements `IPerson`, and deserialize into it then. – Jonathon Reinhart Dec 09 '12 at 20:48
  • @JonathonReinhart why wouldn't he be able to dynamically create a class that implements the said interface, then deserialize into it? Is it impossible to create a custom serializer that only serializes and deserializes data defined by an interface (from an object that implements it)? – neeKo Dec 09 '12 at 21:21
  • @NikoDrašković how do you do such a thing in C#, without using crazy reflection techniques? – Jonathon Reinhart Dec 09 '12 at 21:26
  • @JonathonReinhart [Generate new class source and compile it?](http://stackoverflow.com/a/3862344/1020861) Sounds easy enough, and it could be done once per interface. For custom serializer I would presume you'd need reflection, but only to access properties based on the interface, so nothing too advanced. Only speculating though - haven't tried. Unless there is a reason this cannot be done, it's not exactly *end of story*, is it ;) – neeKo Dec 09 '12 at 21:33
  • @NikoDrašković Not really - I mean, you're still instantiating a class that is not an interface. How that class comes to existence is up to you - but I really recommend against using the CSharpCodeProvider.Compile[anything] for something like this. The performance cannot be good . – Jonathon Reinhart Dec 09 '12 at 21:58

3 Answers3

2

If I understand your question right - since you specified tags "testing", "unit" I believe you are looking for a Mocking Framework which can create a mock for a given interface.

There are a lot of frameworks to use, this is a nice post which can help you make decision:

What C# mocking framework to use?

If you are looking not for a unit testing solution - please remove those tags.

Community
  • 1
  • 1
sll
  • 61,540
  • 22
  • 104
  • 156
1

You can achieve this by using the impromptu interface library.

Create a dynamic object and then use ActLike<IMyInterface>() to get it to behave just like it was an instance of a class that implements IMyInterface.

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
0

Creating mock objects for unit testing is already covered by other answers. This on about "deserialize to interface without knowing actual type".

Deserialization to an interface is not possible for general case. There is no mapping between serialized data and values of properties on interface.

Following sample shows some of the problems:

interface IPerson 
{
  string Name {get;}
}

class Person: IPerson
{
   string realName;
   public string Name 
   {
     get {return realName;} 
     set {realName=value;}
   }
}

class FakePerson : IPerson
{
   public string Name {get {return "Bob";} }
}

Now if you serialized Person you'll not be able to read anything but person as there is no clear mapping between realName and Name. FakePerson is even worse since there is nothing serialized for Name at all.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179