简体   繁体   中英

Reflection or Clone() C#

When, in general, should I be using Reflection versus (shallow) Clone methods?

If all my classes have blank constructors is using reflection fine, or is it better practice to write

Clone() 
{ 
    return new MyClass(); 
} 

methods for classes that need them?

I can see that the Clone method is more type-safe as it can be checked at compile time, but which is faster, and when is it better to use reflection?


Update. I'm not looking for a solution to a particular problem, but rather to learn more theory to understand how to approach problems of this nature.

However, to put it in some context, I'm making a game, and I come across this issue regularly. For example, the game allows the user to "place" certain objects. When clicking repeatedly, the game needs to create multiple instances (of an unknown subclass). I came up with three solutions:

a) Have the menu which selects which object you will place hold a type, and use Reflection to generate that type repeatedly.

b) Have the menu hold an instance of the object, and use Reflection to create new instances. (This could now be replaced with MemberwiseClone)

c) Have the menu hold an instance of the object, and use a Clone method to create new instances after the first is placed.

I assume C is best, but I want to understand why.

The model answer I'm looking for is along the lines of this (please expand, correct and complete as appropriate):

A "Clone" method will typically do more than just call new() as you indicate in your question; it will copy across some or all of the properties of the instance it is cloning. A shallow clone copies a limited number of the properties, and a deep clone copies all of them.

If you simply need to create a new instance of a particular class, it's better to make use of Object.MemberwiseClone().

You should be using Clone methods every time you have a reference to an instance, where you know that instance will have the ICloneable Attribute.

Activator.CreateInstance() should be reserved for use only when you do not have a reference to an instance, for example when if you loaded class information from a text file, or XXXXXXX.

The way to approach the problem of creating new instances of an unknown class is always to attempt to do this via typesafe, compile-time methods (eg Cloning), and only to use Reflection where this is entirely impossible.

As a summary of pros and cons:

Clone/MemberwiseClone

Pros: Checked as typesafe at compile time, will not crash your program at runtime, faster at runtime Cons: Cannot always be used

Reflection

Pros/Cons: Opposite to above.

If you truly want a shallow copy then you could very well use Object.MemberwiseClone and implement the ICloneable interface.

public class Model : ICloneable
{
    public object Clone()
    {
        return MemberwiseClone();
    }
}

You could use reflection to perform a shallow copy if you really wanted to. I understand it to be advantageous as it's automated and can be extended to provide a deep copy. However, reflection is slower than Object.MemberwiseClone() and is not allowed in partial trust environments. It's also more code to implement initially. Read more here .

To create a new instance of a class:

MyClass obj = new MyClass();

To clone an instance:

MyClass clone = obj.Clone();

If you want to make your class Cloneable you should implement the ICloneable interface:

public class MyClass : ICloneable
{
    public MyClass Clone()
    {
        MyClass clone = new MyClass();
        // Copy property values to clone-instance.
        return clone;
    }
}

I cannot figure out why you want to involve Reflection in creating new instances.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM