3

It may sound a little bit complicated but I will try to explain it clearly.

Is it possible in C# if I have a long interface :

public interface IInterface
{
    bool interface_1;
    bool interface_2;
    bool interface_3;
    bool interface_4;
    bool interface_5;
    ...
}

And a class that implements the interface

public class MyClass : IInterface
{
    ...
}

Imagine I have an object of IInterface myInterface, is there a way to create a MyClass object myClass and to set all fields of myClass with the one of myInterface without setting all fields one by one. Like :

IInterface myInterface;
MyClass myClass;
myClass.SetIInterface(myInterface);
hokkos
  • 500
  • 6
  • 17

5 Answers5

2

Imagine I have an object of IInterface myInterface, is there a way to create a MyClass object myClass and to set all fields of myClass with the one of myInterface

This is fundamentally flawed. You cannot have an object of an interface, only an object of a class that implements the interface. Furthermore, an interface cannot have fields, only properties, events and methods. The interface declaration in your snippet can never compile. Furthermore, the odds that the passed object has any fields in common with your class are slim to none, unless they have a base class in common that declares the fields.

You'll need a base class instead of an interface. Copying the field values is now trivial and quick.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

Yes, you can do that using reflection. Use typeof(IInterface).GetProperties() (interfaces cannot define fields) to retrieve a list of properties you'd like to get or set, and use the appropriate methods on the returned object to do that (e.g. PropertyInfo.GetValue() and PropertyInfo.SetValue()).

Allon Guralnek
  • 15,813
  • 6
  • 60
  • 93
1

You can easily do that using reflection. You reflect over all the public properties of the interface, read the value from the passed IInterfaceand write that value into a new object of MyClass:

public interface IInterface
{
    bool interface_1 { get; set; }
}

public class MyClass : IInterface
{

}

public class ReflectionHelper
{
    private MyClass CreateByCopy(IInterface instance)
    {
        MyClass obj = new MyClass();
        foreach (PropertyInfo property in typeof( IInterface ).GetProperties())
        {
            property.SetValue( property, property.GetValue( instance, null ), null );
        }

        return obj;
    }
}

Make sure you handle indexer properties correctly, should you have them. I must warn you though, this approach will have horrible performance. But it should be fine if you are not creating a lot of objects this way.

Johannes Rudolph
  • 35,298
  • 14
  • 114
  • 172
1

This is very similar to this old question - and the answer is the same: if you're doing stuff like this (and dont cite good reasons why its different for you), you're in AutoMapper territory (or are about to write a pale imitation of it)

Community
  • 1
  • 1
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
0

Sure there is. You can define the SetIInterface() method in the class (and/or the interface) and handle the setting of fields one-by-one in one place. You could also define an extension method if you didn't have control over the code for MyClass or IInterface.

KeithS
  • 70,210
  • 21
  • 112
  • 164