C# - How to get the value of a passed lambda expression - c#

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.

Related

Wrapping a Generic class inside a non Generic class C#

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).

.Net arbitratry runtime class instantion and method calling

I am looking for a way to do arbitrary class instantion as well as attribute assignement and possibly method calling in .Net and preferrably C#. Since arbitrary is too broad a word let me tell you what I am after.
Let's say I have a DLL (objects.dll) that contains:
public class Person
{
// Field
public string name;
// Constructor that takes no arguments.
public Person()
{
name = "unknown";
}
// Constructor that takes one argument.
public Person(string nm)
{
name = nm;
}
// Method
public void SetName(string newName)
{
name = newName;
}
}
public class Table
{
// Field
public int width;
public int lenth;
public int height;
// Constructor that takes no arguments.
public Table()
{
width = 0;
length = 0;
height = 0
}
// Constructor that takes three arguments.
public Table(int w, int l, int h)
{
width = w;
length = l;
height = h;
}
// Method
public void SetWLH(int w, int l, int h)
{
width = w;
length = l;
height = h;
}
}
public class Printer
{
public Printer(){}
public void printAPerson(Person p)
{
//do stuff with p
}
public void printATable(Table t)
{
// do stuff with t
}
}
I want to be able to instantiate either of the classes above, set attribute values and call methods at runtime from a different program in the most generic possible. eg. lets say I hame a programm called myprog.exe, i want to be able to do the following
myprog.exe objects.dll Person name testname Printer printAPerson
where:
objects.dll is the dll that contains all required classes
Person is the first I will instantiate name is its attribute
testname is the value I will assign to this attribute
Printer is the class I will use for printing
printAPerson is the method in the Printer class I need to call with the specified object as a parameter.
As you can see, in the best case for my use scenario, neither of the objects and classes are/will be known at compile time so I would like to be as non-casting as possible. If that is not possible I will take what I can.
I have seen this, How to use reflection to call a method and pass parameters whose types are unknown at compile time?, which to my limited knowledge kind of does what I want minus the casting bits or I could be mistaken.
Thanks a lot!
Instead of using Reflection you could use dynamic. But this requires that the Printer class and others are changed. And you would loose intellisense and compile time checks.
public class Printer
{
public Printer() { }
public void printAPerson(dynamic p)
{
//do stuff with p
Console.WriteLine("Person name: " + p.name);
}
public void printATable(dynamic t)
{
// do stuff with t
Console.WriteLine("printATable(Table p) is called");
}
}
public class TestDynamic
{
public static void Test()
{
// To get the type by name,
// the full type name (namespace + type name) is needed
Type personType = Type.GetType("StackOverflowCodes.Person");
object personObj = Activator.CreateInstance(personType);
// Implicit cast to dynamic
dynamic person = personObj;
person.SetName("Alan Turing");
Type printerType = Type.GetType("StackOverflowCodes.Printer");
object printerObj = Activator.CreateInstance(printerType);
dynamic printer = printerObj;
printer.printAPerson(personObj);
}
}
Are you flexible concerning your executable input format? If so, you could do what you want by having a convention. I would do this using a JSON structure like this one:
{
Command : "",
Arguments : {
Argument1 : 0,
Argument2 : { }, // can be another complex object
Argument3 : [] // an empty array maybe ...
}
}
Where Command would be something like "ClassName.MethodName", Arguments will be a JSON object that each object property represents your method parameter.
In your executable, you must parse this JSON using a library (example http://www.newtonsoft.com/json) and use reflection to deserialize every JSON object parameter and call your method. If you cannot get it work, please let me know I will try to make an example (if I will have time, this night because I am at work right now).
For your case you just want to print an object of type Person to the printer right? You could execute a command like this:
{
Command : "Printer.PrintAPerson",
Arguments : {
p : { name : 'george' }
}
}
If you want to rely on a standard protocol, please check the JSON-RPC protocol: http://json-rpc.org/wiki/specification

How to create a "pointer-like" class, easily exposing functions of member?

What I am trying to do is find the most elegant way to create a "pointer-like" class for a specific object/class type that I have in a project.
What I mean is a little confusing without an example. Take this really simple class:
public class MyClass
{
private string _name;
public string GetName() { return _name; }
public void SetName(string name) { _name = name; }
}
I want to create a second class which is like a pointer to it like this:
public class MyClassPtr
{
private MyClass _obj;
public bool IsValid = false;
public MyClassPtr(MyClass obj) { _obj = obj; IsValid = true; }
public void InvalidatePtr()
{
IsValid = false;
obj = null;
}
// SOME MAGIC HERE?
}
The challenge: The key is that I want to elegantly have MyClassPtr provide an interface to all of the public methods/members in MyClass without writing wrappers and/or accessors around each method/member.
I know that I could do this:
public class MyClassPtr
{
public string GetName() { return _obj.GetName(); }
...
}
But that's what I want to avoid. Is there some fundamental abstraction that I don't know of that I can apply to MyClassPtr to allow it to easily re-expose the methods/members in MyClass directed through _obj? I do NOT want MyClassPtr to inherit MyClass. Should MyClassPtr be a type instead, and some trick with accessors to expose the methods/members of MyClass?
Edit: More context on why I am looking for such a design through an example. Here is the overall goal. Imagine a platform that parses through data about people and when it finds information about a person, it creates an instance of Person with that information. You could get a handle to that person like:
Person person1 = platform.GetPerson(based_on_data);
Now, imagine the platform had two instances of Person that it thought were different people, but all of a sudden information came in that strongly suggested those two instances actually refer to the same person. So, the platform wants to merge the instances together in to a new object, let's call it personX.
Now, floating around in the platform someone had a copy of one of those two instances that got merged, which was person1. What I want to do is on-the-fly replace person1 with personX. Literally, I want person1==personX to be true, NOT just that they are two different objects with the same data. This is important since the platform could make a change to personX and unless the two objects are literally equal, a change to personX would not be automatically reflected in person1.
Since I can't on-the-fly replace person1 with personX I had that idea that I wouldn't give direct access to Person, instead I would give access to PersonPtr which the platform (on-the-fly) can change what Person it is pointing to. This would insurance that once person1ptr gets updated to point to personX, if a change is made in personX it will be seen in person1ptr
You could of course use something like
public class MyClassWrapper
{
MyClass _obj;
public MyClassWrapper(MyClass obj)
{
_obj = obj;
}
public void Invoke(Action<MyClass> action)
{
action(_obj);
}
public U Invoke<U>(Func<MyClass, U> func)
{
return func(_obj);
}
public void ChangeTo(MyClass obj)
{
_obj = obj;
}
}
Given your class looks like
public class MyClass
{
public string Name { get; set; }
}
Example:
var person1 = new MyClass { Name = "Instance1" };
var person2 = new MyClass { Name = "Instance2" };
var wrapper = new MyClassWrapper(person1);
wrapper.Invoke(x => x.Name += "original");
var x = wrapper.Invoke(x => x.Name); // Instance1original
wrapper.ChangeTo(person2);
var y = wrapper.Invoke(x => x.Name); // Instance2
but it has a major drawback: you can't access members directly, so you can't bind the data (to a DataTable or a Control).
It would be better to implement all members of your class also in your wrapper class. If you're afraid changes in your class will be forgotten to be implemented in your wrapper, just use an interface:
public interface IMyClass
{
string Name { get; set; }
}
public class MyClass : IMyClass
{
public string Name { get; set; }
}
public class MyClassWrapper: IMyClass
{
MyClass _obj;
public MyClassWrapper(MyClass obj)
{
_obj = obj;
}
public string Name
{
get { return _obj.Name; }
set { _obj.Name = value; }
}
}
Note that regardless which approach you use, you'll have to always keep a reference to the wrapper instance to actually change the underlying instance (using something like static aside).
Also, changing the underlying instance of such a wrapper without telling the component using it that it changed don't seem to be a good idea. Maybe your system is simple enough to get away with a wrapper; that's something you have to decide for yourself.
Maybe your wrapper should simply have an Invalid flag (and/or use an event to signal a change of the underlying object.). Once the underlying object is merged, it is set to true and each member access should throw an exception. This would force the component using the wrapper to deliberately react to changes and to reload the data from your service.
All in all, I think using such a wrapper will just clutter up your code and be error prone (just imagine adding multithreading to the mix). Think twice if you really need this wrapper.
Why not just simply ask your service for a new instance of your class everytime you use it (the service can simply use a cache)? Sure, you can't prevent that someone somewhere keeps a reference; but at least you'll keep your sanity.

"Writable" reference to object

Not sure I'm able to formulate this question in a way someone would simply understand, so lets have a cool marketing example:
public class Part
{
public MemberType member;
...
}
public class Product
{
public Part part1;
...
}
...
Product product = new Product();
I need to modify the public product's part1. So, the natural method is to write something like:
product.part1 = new Part();
Now, an algorithm (let's say a sort of search one) would go through the product object and identify the part1 as an interesting part and returns reference to it:
Part Search(Product product)
{
Part part = null;
...
part = product.part1;
...
return part;
}
...
interesting_part = Search(product);
We can alter the product object via the interesting_part like
interesting_part.member = whatever;
Now, the question: in c/c++ if the Product.part1 is pointer to Part and Search returns address of this pointer, we could replace the part1 just by assigning new value to this address. AFAIK this is not possible for c# reference:
interesting_part = new Part();
Just creates new object and copies its reference to the interresting_part, but without knowing the member parent (product object), we are not able to modify the (product.part1) reference, just its content. We would need second level of the reference.
Is there something like "ref reference" type which would accept reference addresses? In such hypothetical case the search would return ref Part and assigning to such value would replace the referenced object with the new one.
Thanks.
You could create a Reference class
class Reference<T>
{
private Func<T> m_Getter;
private Action<T> m_Setter;
public Reference(Func<T> getter, Action<T> setter)
{
m_Getter = getter;
m_Setter = setter;
}
public T Value
{
get{return m_Getter();}
set{m_Setter(value);}
}
}
Now you can say
Reference<Part> Search(Product product)
{
Part part = null;
...
part = product.part1;
var reference=new Reference<Part>(()=>product.part, (value)=>product.part1=value);
return refernce;
}
var partReference = Search(product);
partReference.Value = someNewPart;
In a very similar situation, I keep a reference of the parent in each child object. Simple and works.
public class Part
{
public MemberType member;
...
public Product parent;
Part(Product p)
{
parent = p;
}
}
public class Product
{
public Part part1;
...
}
I don't think you can do that. You would need to mutate a reference to you product object, or have some other added layer of reference.
So you need to build a Proxy object. The Product would get a reference to the Proxy and the (hidden) Part can be exchanged. This is a common OO design pattern. Of course the Proxy can delegate method calls to the Part.
If you want to change the field, you can do this,
class Program
{
static void Main(string[] args)
{
var holder = new Holder();
holder.CurrentPart = new Part() { Name = "Inital Part" };
Console.WriteLine(holder.CurrentPart.Name);
TestRef(ref holder.CurrentPart);
Console.WriteLine(holder.CurrentPart.Name);
Console.ReadKey();
}
public static void TestRef(ref Part part)
{
part = new Part() { Name = "changed" };
}
}
public class Part
{
public string Name;
}
public class Holder
{
public Part CurrentPart;
}
This won't work with property, indexers and so.
As far as I know, there isn't any way to alter an object's "parent" without having a reference to it. So I believe the official answer to your question as written is "no".
That said, there are many ways to accomplish the task as written. The easiest option is to add a reference to the parent from the part object. You end up with something like:
public class Part
{
public Product parentProduct;
public MemberType member;
...
}
Now whenever you have a part object you also know what product the part goes with (IF it does indeed go with a part at all). This is not necessarily a bad coding style but there certainly are pitfalls. You can update the product, but forget to update the parts in that product, you are coding so that parts have one product, but what if that part has many products? You can see how this works, but it can get complicated.
Taking this and making it more generic you can have reference the parent as an object type. That looks like:
public class Part
{
public object parent;
public MemberType member;
...
}
Now when you want to use the parent you can write something like:
var parentProduct = myPart.parent as Product;
This will convert the parent to a product or will assign null if the parent is not of the type Product. Now parts can have parents of any given type you would want and you have made the pattern more flexible.
One final pattern I know people use frequently is delegates. This allows you to pass in a function effectively modifying the way "search" is working. Say what you really want to do is search, then process the results in some manner, but you want that processing to be flexible (this may be what you were doing with the results). In that case, you can use delegates as follows:
// define the delegate
public delegate void ProcessResultDelegate(Product result, Part interestingPart);
// an example search function
public static void RunSearch(IEnumerable<Product> products, ProcessResultDelegate processingHelper)
{
// run the search... then call the processing function
processingHelper(searchResult, interestingPart);
}
This pattern is more useful when you want to modify the behavior of a routine rather than the return value from that routine.
Anyways, hope these patterns help some!

Invalid Cast Exception when using an interface as generic type

So I am not sure if this is a C# generics issue I don't understand or whether it has something to do with the Unity compiler not being able to handle it. I come from C++ and am slightly experienced with c# but not with generics.
So onto the issue:
I have a generic class called TurnOrderQueue. in short it is a custom Queue that orders things based on some values that are passed in with it. (It is in a namespace called TurnOrderQueueNS)
I have an empty(for now) interface called IActor which is to represent things that can take actions in a turn based game.
Finally I have a TurnOrderManager that handles everything with turn order and owns a TurnOrderQueue.
When I attempt to instantiate the TurnOrderQueue using IActor as the type I get an invalid cast exception
public TurnOrderQueueNS.TurnOrderQueue<IActor> TurnOrder = new TurnOrderQueueNS.TurnOrderQueue<IActor>();
The Exception I get at runtime is:
InvalidCastException: Cannot cast from source type to destination type.
TurnOrderQueueNS.TurnOrderQueue`1[IActor]..ctor ()
TurnManager..ctor ()
Can you not use Interfaces as the type with generics in C# or am i missing something?
Per Request Here is the TurnOrderQueue.cs
edit: removed much of the class to show the meat of the issue
public class TurnOrderQueue<t>: IComparer{
private SortedList<QueueItem<t>,t> turnOrderList;
private int currentTurnIndex = 0;
#region IComparer implementation
int IComparer.Compare (object x, object y)
{
return Compare ((QueueItem<t>)x, (QueueItem<t>)y );
}
#endregion
}
public struct QueueItem<t>
{
public int priorityValue;
public int tieBreakerValue;
public t item;
}
}
Exception you get is not related to IActor interface or generic parameter type.
In constructor you are trying to cast this to IComparer<QueueItem<t>>.
To be able to do that your class should implement IComparer<QueueItem<t>>.
public class TurnOrderQueue<t> : IComparer<QueueItem<t>>
{
<skipped>
public TurnOrderQueue()
{
turnOrderList = new SortedList<QueueItem<t>, t>(this);
}
public int Compare(QueueItem<t> item1, QueueItem<t> item2)
{
<skipped>
}
}

Categories

Resources