Reputation: 13
I have the following problem. I want to be able to set this to the passed reference without using reflection, can this be done?
//partial class of a Linq-to-SQL class
public partial class Product
{
public Product (Product product, List<ProductAttributes> productAttributes)
{
// without individually setting all the properties
//then just set any other properties
this.ProductAttributes = productAttributes;
}
}
Upvotes: 1
Views: 104
Reputation: 185703
You cannot set this
in reference types (and setting it in value types is probably not something to be condoned). If what you're looking to do is a shallow (or deep) copy of the object's fields or properties, then you have two choices:
Neither the language nor the runtime have any built-in facility for automating shallow or deep copying, and for good reason. It would be impossible to determine what that would look like for any given type.
Upvotes: 1
Reputation: 49013
You can't assign anything to this
in a class as it's read-only.
I suspect you wanted to do so because of a bad design. You probably should post what you actually want, not how you would like to do it so we can provide a more appropriate answer.
Upvotes: 2
Reputation: 437824
Sorry, but this is not possible to do magically. You either need to use reflection, or to write a method that manually does the copying. There are, of course, multiple libraries that can do this for you.
Upvotes: 0
Reputation: 1503629
You can't, basically.
You can only set this
in structs, and even then it's usually a bad idea :)
If you can explain what higher-level goal you're trying to achieve, we may be able to help more - but you simply can't change the value of this
for a reference type, with or without reflection.
Upvotes: 3