jeudi 27 avril 2017

C# Instantiate a class of any type inside another object and call a method?

apologise in advance if I’m not using correct language here.

I’m learning C# and I’ve got fairly comfortable with threading, locking threads and using ReaderWriterLockSlim. – Purely to indicate a watermark on my learning.

I’m currently trying to write a class that perform EXECUTE-AROUND pattern for any class that is passed to it.

Using rough pseudo code something like this:

class MyWrapperClass(){
    private object containedObject;

    public void importObject<MyObject>();
    {
        where MyObject: new ()
        {
            containedObject = new MyObject();
        }
    }

    public void callMethod(Func<int> MyMethod)
    {
        //Wrapped Code here
        containedObject.MyMethod;
        //Wrapped Code here
    }
}

The objective is that any object type can be passed to my MyWrapper Class – calls to this object can then only be done by via callMethod, the goal is to wrap code before and after MyMethod.

I have seen that I could do this though reflection but that would rely on me passing the method names as strings to the MyWrapperClass and doesn’t seem like the best way to do it.

The goal – is to write agnostic class that handles converting all the methods to new threads, the object would then block any further calls with the wrapped code until the current method is complete.

The purpose – is to quickly turn any object into thread safe action. I’ve done this on per object biases for specific functions, but it would be good to be able to have something that does reasonable amount of housekeeping and not duplicate that code.





Aucun commentaire:

Enregistrer un commentaire