I have a special case. I need to rewrite a library and I do not have the original code. To make the discussion clearer lets call the library HandlerLibOld and HandlerLibNew.
I want to make HandlerLibNew that shall replace HandlerLibOld.
I have an application that uses this library, and it is big application with 1000s of calls to the library.
I rather not have to change the application code.
Sample application code
public class MyMessage : MyBaseClass
{
public int ValueA {get;set;}
public int ValueB {get;set;}
}
public class MyOtherMessage : MyBaseClass
{
public int ValueC {get;set;}
public int ValueD {get;set;}
}
public class Sender
{
Handler handler = new Handler();
handler.Send<MyMessage>(m => { m.ValueA=10; m.ValueB=20; });
}
Notice the line handler.Send<MyMessage>(m => { m.ValueA=10; m.ValueB=20; });. It uses a lambda expression to define an object of the MyMessage and sets two properties of this object. Notice that I rather not have to change this code.
Maybe I have misunderstood what the lambda expression does, but the point is I need to tell the Handler.Send about the values ValueA and ValueB.
Now I want to write the implementation of the Send method of the Handler. The tricky part is that the lambda expression makes it an action, but I want to get the MyMessage object instance. How can I achieve that? Do I need to use reflection somehow?
My attempts
I have tried:
public class Handler
{
public void Send<T>(Action<T> actionmessage)
{
// actionmessage is an object of type Action<T>,
// I want to get the object of type T that was passed into the method.
}
}
I have also tried:
public class Handler
{
public void Send<T>(T message)
{
// This will not compile. I get:
// Cannot convert lambda expression
// to type 'object' because it is not a delegate type
}
}
I know it is possible somehow because this works with the HandlerLibOld. How can I achieve the same with HandlerLibNew? I don't have access to the code of HandlerLibOld.
In order for that Action to work, you must have an instance of T to begin with:
public void Send<T>(Action<T> actionMessage)
where T: new()
{
var input = new T();
actionMessage(input);
//T has now valueA and valueB populated
//do more with input T
//send
}
Now if T were to have a constraint such as implementing a certain interface, you can do more stuff on the object itself. If you are using base classes you must be careful with object slicing.
I'm working on a C# console application. My objective is to create an object called GroupEntity, preferably of non generic type.
Inside this GroupEntity object will be a List of 'AttributeFilter' object which contains object of Generic type which hold the attribute name on a user object in Active Directory and the possible values of those user objects. The reason I want the AttributeFilter object to take a generic type is because some attributes on user objects in AD are string, some are int32, some are int64 etc.
Here are my classes (I've cut out the contructorse etc to save space here)
public class AttributeFilter<T> : IEqualityComparer<AttributeFilter<T>>
{
private string _attributeName;
private T _attributeValue;
private List<T> _attributeValues { get; set; }
public AttributeFilter(string attributeName)
{
AttributeName = attributeName;
_attributeValues = new List<T>();
}
public void AddValues(T attributeValue)
{
AttributeValue = attributeValue;
if (!_attributeValues.Contains(AttributeValue))
{
_attributeValues.Add(AttributeValue);
}
}
// Ive cut out the getter setter etc that is not relevant
}
Here is the GroupEntity class. Notice I have a
List<AttributeFilter<T>>
field. Problem is I dont know what that T will be until I run program.cs
public class GroupEntity<T>
{
private string _groupName;
// because I want to a have a List<AttributeFilter<T>>, but I dont really want this here. because of program.cs when I initialise a new GroupEntity<> I have to tell it what type. I wont know. The type could be int32, string, long or whatever.
private List<AttributeFilter<T>> _filters;
public void AddFilters(AttributeFilter<T> attributeFilter)
{
if (!_filters.Contains(attributeFilter, attributeFilter))
{
_filters.Add(attributeFilter);
}
}
public GroupEntity()
{
_filters = new List<AttributeFilter<T>>();
}
public GroupEntity(string groupName) : this()
{
_groupName = groupName;
}
}
Now I use program.cs to initialise and test...
class Program
{
static void Main(string[] args)
{
// Create AttributeFilter object for user attribute: EYAccountType
var at1 = new AttributeFilter<string>("EYAccountType");
at1.AddValues("02");
at1.AddValues("03");
at1.AddValues("04");
at1.AddValues("05");
// try adding anothr AtributeFilter with same name.
var at3 = new AttributeFilter<string>("EYAccountType1");
at3.AddValues("06");
at3.AddValues("07");
// Create AttributeFilter object for user attribute: userAccountControl
var at2 = new AttributeFilter<int>("userAccountControl");
at2.AddValues(512);
at2.AddValues(544);
at2.AddValues(546);
at2.AddValues(4096);
// Now create a GroupEntity object
var group1 = new GroupEntity<string>("My_First_AD_Group_Name");
// Try adding the above two AttributeFilter objects we created to the GroupEntity object.
group1.AddFilters(at1);
group1.AddFilters(at3);
// This is the problem. I know why this is happening. because I initialised the var group1 = new GroupEntity<string>. So it wont accept at2 because at2 is taking in int.
//group1.AddFilters(at2);
}
So how can I write my GroupEntity class without a generic parameter so I can hold various types of AttributeFilter<T> inside it. So for example, I can hold AttributeFilter<int> and AttributeFilter<string> and AttributeFilter<long>
I can't seem to figure out this problem.
More or less you can't.
Generic types instantiated with different types has no relationship to each other (i.e. AttributeFilter<long> and AttributeFilter<int> don't get any common base class - they are as differnet as Exception and HttpClient). So there is no way to put instances of such types into single collection with strong typing.
Standard solution - use non-generic base class or interface for your AttributeFilter<T> type. Alternatively - store them as collection of object and lose all type safety, or maybe collection dynamic which at least give you chance to call methods (at cost of reflection).
I'm creating a child object from a parent object. So the scenario is that I have an object and a child object which adds a distance property for scenarios where I want to search. I've chosen to use inheritance as my UI works equivalently with either a search object or a list of objects not the result of a location search. So in this case inheritance seems a sensible choice.
As present I need to generate a new object MyObjectSearch from an instance of MyObject. At present I'm doing this in the constructor manually by setting properties one by one. I could use reflection but this would be slow. Is there a better way of achieving this kind of object enhancement?
Hopefully my code below illustrates the scenario.
public class MyObject {
// Some properties and a location.
}
public class MyObjectSearch : MyObject {
public double Distance { get; set; }
public MyObjectSearch(MyObject obj) {
base.Prop1 = obj.Prop1;
base.Prop2 = obj.Prop2;
}
}
And my search function:
public List<MyObjectSearch> DoSearch(Location loc) {
var myObjectSearchList = new List<MyObjectSearch>();
foreach (var object in myObjectList) {
var distance = getDistance();
var myObjectSearch = new MyObjectSearch(object);
myObjectSearch.Distance = distance;
myObjectSearchList.add(myObjectSearch);
}
return myObjectSearchList;
}
The base class needs to define a copy constructor:
public class MyObject
{
protected MyObject(MyObject other)
{
this.Prop1=other.Prop1;
this.Prop2=other.Prop2;
}
public object Prop1 { get; set; }
public object Prop2 { get; set; }
}
public class MyObjectSearch : MyObject
{
public double Distance { get; set; }
public MyObjectSearch(MyObject obj)
: base(obj)
{
this.Distance=0;
}
public MyObjectSearch(MyObjectSearch other)
: base(other)
{
this.Distance=other.Distance;
}
}
This way the setting of properties is handled for all derived classes by the base class.
You can use reflection to copy properties.
public class ChildClass : ParentClass
{
public ChildClass(ParentClass ch)
{
foreach (var prop in ch.GetType().GetProperties())
{
this.GetType().GetProperty(prop.Name).SetValue(this, prop.GetValue(ch, null), null);
}
}
}
There is no easy way to do this, unfortunately. As you said, you would either have to use reflection, or create a "Clone" method that would generate a new child object using a parent object as input, like so:
public class MyObjectSearch : MyObject {
// Other code
public static MyObjectSearch CloneFromMyObject(MyObject obj)
{
var newObj = new MyObjectSearch();
// Copy properties here
obj.Prop1 = newObj.Prop1;
return newObj;
}
}
No matter what, you're either going to end up writing reflection code (which is slow), or writing each property out by hand. It all depends on whether or not you want maintainability (reflection) or speed (manual property copy).
A generic solution would be to serialize it to json and back. In the json-string is no information about the class name from which it was serialized.
Most people do this in javascript.
As you see it works well for pocco objects but i don't guarantee that it works in every complex case. But it does event for not-inherited classes when the properties are matched.
using Newtonsoft.Json;
namespace CastParentToChild
{
public class Program
{
public static void Main(string[] args)
{
var p = new parent();
p.a=111;
var s = JsonConvert.SerializeObject(p);
var c1 = JsonConvert.DeserializeObject<child1>(s);
var c2 = JsonConvert.DeserializeObject<child2>(s);
var foreigner = JsonConvert.DeserializeObject<NoFamily>(s);
bool allWorks = p.a == c1.a && p.a == c2.a && p.a == foreigner.a;
//Your code goes here
Console.WriteLine("Is convertable: "+allWorks + c2.b);
}
}
public class parent{
public int a;
}
public class child1 : parent{
public int b=12345;
}
public class child2 : child1{
}
public class NoFamily{
public int a;
public int b = 99999;
}
// Is not Deserializeable because
// Error 'NoFamily2' does not contain a definition for 'a' and no extension method 'a' accepting a first argument of type 'NoFamily2' could be found (are you missing a using directive or an assembly reference?)
public class NoFamily2{
public int b;
}
}
If a shallow copy is enough, you can use the MemberwiseClone method.
Example:
MyObject shallowClone = (MyObject)original.MemberwiseClone();
If you need a deep copy, you can serialize/deserialize like this: https://stackoverflow.com/a/78612/1105687
An example (assuming you write an extension method as suggested in that answer, and you call it DeepClone)
MyObject deepClone = original.DeepClone();
I first came accros this question when I was looking for doing this.
If you are able to work with C# 9 and record-classes. You only have to create a new constructor in the sub-class taking in a base class object and hand it over to the subclass:
public record MyObject {
...
}
public record MyObjectSearch :MyObject
{
public MyObjectSearch(MyObject parent) : base(parent) { }
...
}
Then you can create the child object like this:
MyObject parent = new();
MyObjectSearch m = new MyObjectSearch(parentObj) { Distance = 1.1};
Credits to https://stackoverflow.com/a/64573044/2582968
Seems natural for the base object to have constructor with parameters for its properties:
public class MyObject
{
public MyObject(prop1, prop2, ...)
{
this.Prop1 = prop1;
this.Prop2 = prop2;
}
}
So then, in your descendant object you can have:
public MyObjectSearch(MyObject obj)
:base(obj.Prop1, obj.Prop2)
This reduces duplication related to assignments. You could use reflection to automatically copy all properties, but this way seems more readable.
Note also, that if your classes have so much properties that you're thinking about automatizing of copying of the properties, then they are likely to violate the Single Responsibility Principle, and you should rather consider changing your design.
There are libraries to handle this; but if you just want a quick implementation in a few places, I would definitely go for a "copy constructor" as previously suggested.
One interesting point not mentioned is that if an object is a subclass, then it can access the child's private variables from the within the parent!
So, on the parent add a CloneIntoChild method. In my example:
Order is the parent class
OrderSnapshot is the child class
_bestPrice is a non-readonly private member on Order. But Order can set it for OrderSnapshot.
Example:
public OrderSnapshot CloneIntoChild()
{
OrderSnapshot sn = new OrderSnapshot()
{
_bestPrice = this._bestPrice,
_closed = this._closed,
_opened = this._opened,
_state = this._state
};
return sn;
}
NOTE: Readonly member variables MUST be set in the constructor, so you will have to use the child constructor to set these...
Although I don't like "up-sizing" generally, I use this approach a lot for analytic snapshots...
In Excel object model 'Thisworkbook' is a class that has 'Worksheets' class in it and can be overloaded with index or by sheet name. Moreover, 'Worksheets' class also has some properties/methods that can be called with/without calling the constructor (default).
E.g. MsgBox ThisWorkbook.Worksheets(1).Name '<-- by (int)
E.g. MsgBox ThisWorkbook.Worksheets("Sheet1").Name '<- by (string)
E.g. Msgbox Thisworkbook.Worksheets.Count '<- 'count property
How can I create such class in C# that has another class in it and can be overloaded?
I have tried creating a public variable of the 2nd class in the 1st class but I can only call default constructor (or other - but just one at a time)
class A
{
public B b = new B(); // <-- this is 1st constructor but how to overload ?
}
class B
{
public B() // Default Constructor
{
}
public B(int i) // Constructor 2
{
}
public B(string s) // Constructor 3
{
}
public int Count // Property
{
get { return 0; }
}
}
//------------------------------------------------------------
class Main()
{
A a = new A();
a.b.Count(); // <-- THIS WORKS
// a.b(1).Name; <-- HOW TO, USING THE SAME CLASS?
// a.b("A").Name; <-- HOW TO, USING THE SAME CLASS?
}
I know that I have not created an object array - 1 or "A" that can be returned in this example, but I just need the concept (you got it right?). I have also seen few examples using List but I really want to know how I can achieve such object hierarchy in C#... (please excuse me if this is a wrong question).
You're misunderstanding operators.
What you're seeing is the indexing operator defined on the Worksheets class.
In C#, that would be called as a.b[1] or a.b["2"].
You define it like this:
public SomeType this[int index] { get { ... } }
public SomeType this[string index] { get { ... } }
I am wanting to access the custom attributes on a Field in the class. I want to access the attributes placed on the field during the fields constructor. Is this possible?
Edit 06/28/09
Something like the below pseudo code
class SpecialInt
{
int _intVal;
int _maxVal;
public SpecialInt()
{
//Get attribute for the instantiated specialint
_maxVal = GetAttribute("MaxValue")
}
}
class main()
{
[MaxValue(100)]
SpecialInt sInt;
public main()
{
sInt = new SpecialInt()
}
}
Sure this is possible. Attributes are stored in Metadata and this is easily accessible during construction of an object.
public class Foo {
[Something]
public int Field1;
public Foo() {
FieldInfo fi = typeof(Foo).GetField("Field1");
SomethingAttribute si = (SomethingAttribute)fi.GetCustomAttribute(typeof(SomethingAttribute),false)[0];
// grab any Custom attribute off of Fiield1 here
}
}
You can test them from anywhere. Attributes are inserted into the metadata for the type when you compile it. A type doesn't need to be instantiated to access field properties.