archwhite
7/5/2018 - 4:07 AM

AOP in C# projects using MrAdvice package

using AOP;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
\!h [assembly: MrAdvice()]
[assembly: AssemblyTitle("WebApplication1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("WebApplication1")]
[assembly: AssemblyCopyright("Copyright ©  2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]


// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7f264a08-f35b-4b95-9902-5abb832290d2")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Utils
{
    public class JSON
    {
        public static string to(object o)
        {
            return JsonConvert.SerializeObject(o);
        }
    }

    public class Memory
    {
        public static long Sizeof(object o)
        {
            long size = 0;
            using (Stream s = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(s, o);
                size = s.Length;
                return size;
            }
        }
    }
}
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace WebApplication1.Controllers
{
    public class ValuesController : ApiController
    {
        public static List<int> List { get; set; } = new List<int>();
        public List<int> list { get; set; } = new List<int>();
        public static int count = 0;

        // GET api/values
        public object Get()
        {
            List.Add(++count);
            list.Add(count);

            return Json(new { Value1 = "value1", Value2 = "value2" });
        }
    }
}
using ArxOne.MrAdvice.Advice;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using Utils;

namespace AOP
{
    [AttributeUsage(AttributeTargets.Assembly)]
    public class MrAdviceAttribute : Attribute, IMethodAdvice
    {
        public MrAdviceAttribute() { }

        public void Advise(MethodAdviceContext context)
        {
            try
            {
                var tname = context.TargetName;
                var fname = context.TargetType.FullName;
                System.Diagnostics.Debug.WriteLine($"ASPECT INFO: {fname + tname}");
                context.Proceed();
                if (tname == "Get")
                {
                    Type type = context.TargetType;
                    long totalsize = 0;
                    foreach (var p in type.GetProperties())
                    {
                        try
                        {
                            var v = p.GetValue(context.Target);
                            var varsize = Memory.Sizeof(v);
                            totalsize += varsize;
                            Debug.WriteLine($"VAR {p.Name} = {JSON.to(v)} size = {varsize}");
                        }
                        catch { };
                    }
                    Debug.WriteLine($"total size = {totalsize}");

                    var result = context.ReturnValue;
                }
            }
            catch(Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }

    }
}