HasQuestionsAndAnswers
HasQuestionsAndAnswers

Reputation: 155

Is there a way to track when a method of some metadata has been called?

I'm using a NuGet Package called DevExpress.Xpo and its classes are uneditable because they're locked as "metadata". The package contains a class called DataStorePool, and, during runtime, I need to somehow be able to log whenever one of its methods is called. For example, when the AcquireChangeProvider method is called, I want to log that it's been called (with a simple Console.WriteLine), but I can't directly add any code to the method itself because it's uneditable. I think I need some way to "listen" to when the method is called, but how do I achieve this?

Upvotes: 0

Views: 150

Answers (1)

Marius
Marius

Reputation: 1574

You can wrap any package with your own class.

Here is a simplified example:

Your class

public class DictionaryWrapper<TKey, TValue> : Dictionary<TKey, TValue>
{
    public DictionaryWrapper() : base() {
        Console.WriteLine("A New Dictionary was created");
    }

    public new void Add(TKey key, TValue value)
    {
        Console.WriteLine($"A value is being added to a dictionary.{Environment.NewLine}\tKey: {key}{Environment.NewLine}\tValue:{value}");

        base.Add(key, value);

        Console.WriteLine($"A new dictionary value was added.{Environment.NewLine}\tKey: {key}{Environment.NewLine}\tValue:{value}");
    }
}

Execute code that uses your new class and method.

var dictionary = new DictionaryWrapper<string, int>();

dictionary.Add("SomeValue", 1);

Console.WriteLine(dictionary.First().Key);
Console.WriteLine(dictionary.First().Value);

Upvotes: 0

Related Questions