namespace ConsoleApplication
{
using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using Microshaoft;
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class PerformanceCountersCategoryCreater
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
//[STAThread]
static void Main(string[] args)
{
string assemblyPath = args[0];
string typeName = args[1];
string category = args[2];
var assembly = Assembly.LoadFile(assemblyPath);
var typesList = assembly.GetTypes().Where
(
(x) =>
{
return (x.Name.ToLower().IndexOf(typeName.ToLower()) >= 0);
}
).ToList();
typesList.ForEach
(
(x) =>
{
PerformanceCountersHelper.CreatePerformanceCountersCategory
(
category
, x
);
}
);
Console.WriteLine("Hello World");
Console.WriteLine(Environment.Version.ToString());
Console.ReadLine();
}
}
}
namespace Microshaoft
{
using System;
using System.Linq;
using System.Diagnostics;
using System.Reflection;
using System.Collections.Generic;
//interface IPerformanceCountersManager
//{
// public string PerformanceCountersInstanceName { set; get; }
// public string PerformanceCountersCategoryName { set; get; }
//}
public static class PerformanceCountersHelper
{
public static void CreatePerformanceCountersCategory
(
string performanceCountersCategoryName
, Type type
)
{
//var type = typeof(T);
var propertiesList = type.GetProperties().ToList();
propertiesList = propertiesList.Where
(
(pi) =>
{
return (pi.PropertyType == typeof(PerformanceCounter));
}
).ToList();
if (PerformanceCounterCategory.Exists(performanceCountersCategoryName))
{
propertiesList.ForEach
(
(pi) =>
{
if (PerformanceCounterCategory.CounterExists(pi.Name, performanceCountersCategoryName))
{
//if (PerformanceCounterCategory.InstanceExists(performanceCountersInstanceName, category))
//{
// //var pc = new PerformanceCounter(category, pi.Name, instanceName, false);
// //pc.InstanceName = instanceName;
// //pc.RemoveInstance();
//}
}
}
);
PerformanceCounterCategory.Delete(performanceCountersCategoryName);
}
var ccdc = new CounterCreationDataCollection();
propertiesList.ForEach
(
(pi) =>
{
var propertyName = pi.Name;
var ccd = PerformanceCounterHelper.GetCounterCreationData
(
propertyName
, PerformanceCounterType.NumberOfItems64
);
ccdc.Add(ccd);
}
);
PerformanceCounterCategory.Create
(
performanceCountersCategoryName,
string.Format("{0} Category Help.", performanceCountersCategoryName),
PerformanceCounterCategoryType.MultiInstance,
ccdc
);
}
public static void AttachPerformanceCountersToProperties<T>
(
string performanceCounterInstanceName
, string category
, T target = default(T)
)
{
var type = typeof(T);
var propertiesList = type.GetProperties().ToList();
propertiesList = propertiesList.Where
(
(pi) =>
{
return (pi.PropertyType == typeof(PerformanceCounter));
}
).ToList();
propertiesList.ForEach
(
(pi) =>
{
var propertyName = pi.Name;
var pc = new PerformanceCounter()
{
CategoryName = category
, CounterName = propertyName
, InstanceLifetime = PerformanceCounterInstanceLifetime.Process
, InstanceName = performanceCounterInstanceName
, ReadOnly = false
, RawValue = 0
};
if (pi.GetGetMethod().IsStatic)
{
var setter = DynamicPropertyAccessor.CreateSetStaticPropertyValueAction<PerformanceCounter>(type, propertyName);
setter(pc);
}
else
{
if (target != null)
{
var setter = DynamicPropertyAccessor.CreateSetPropertyValueAction<PerformanceCounter>(type, propertyName);
setter(target, pc);
}
}
}
);
}
}
}
namespace Microshaoft
{
using System;
using System.Diagnostics;
public static class PerformanceCounterHelper
{
public static CounterCreationData GetCounterCreationData(string counterName, PerformanceCounterType performanceCounterType)
{
return new CounterCreationData()
{
CounterName = counterName
, CounterHelp = string.Format("{0} Help", counterName)
, CounterType = performanceCounterType
};
}
}
}
namespace Microshaoft
{
using System;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
public class DynamicPropertyAccessor
{
private static Assembly GetAssemblyByTypeName(string typeName)
{
return AppDomain.CurrentDomain.GetAssemblies().First
(
(a) =>
{
return a.GetTypes().Any
(
(t) =>
{
return (t.FullName == typeName);
}
);
}
);
}
public static Func<object, object> CreateGetPropertyValueFunc(string typeName, string propertyName, bool isTypeFromAssembly = false)
{
Type type;
if (isTypeFromAssembly)
{
var assembly = GetAssemblyByTypeName(typeName);
type = assembly.GetType(typeName);
}
else
{
type = Type.GetType(typeName);
}
return CreateGetPropertyValueFunc(type, propertyName);
}
public static Func<object, object> CreateGetPropertyValueFunc(Type type, string propertyName)
{
var target = Expression.Parameter(typeof(object));
var castTarget = Expression.Convert(target, type);
var getPropertyValue = Expression.Property(castTarget, propertyName);
var castPropertyValue = Expression.Convert(getPropertyValue, typeof(object));
var lambda = Expression.Lambda<Func<object, object>>(castPropertyValue, target);
return lambda.Compile();
}
public static Func<object, TProperty> CreateGetPropertyValueFunc<TProperty>(string typeName, string propertyName, bool isTypeFromAssembly = false)
{
Type type;
if (isTypeFromAssembly)
{
var assembly = GetAssemblyByTypeName(typeName);
type = assembly.GetType(typeName);
}
else
{
type = Type.GetType(typeName);
}
return CreateGetPropertyValueFunc<TProperty>(type, propertyName);
}
public static Func<object, TProperty> CreateGetPropertyValueFunc<TProperty>(Type type, string propertyName)
{
var target = Expression.Parameter(typeof(object));
var castTarget = Expression.Convert(target, type);
var getPropertyValue = Expression.Property(castTarget, propertyName);
var lambda = Expression.Lambda<Func<object, TProperty>>(getPropertyValue, target);
return lambda.Compile();
}
public static Func<TProperty> CreateGetStaticPropertyValueFunc<TProperty>(string typeName, string propertyName, bool isTypeFromAssembly = false)
{
Type type;
if (isTypeFromAssembly)
{
var assembly = GetAssemblyByTypeName(typeName);
type = assembly.GetType(typeName);
}
else
{
type = Type.GetType(typeName);
}
return CreateGetStaticPropertyValueFunc<TProperty>(type, propertyName);
}
public static Func<TProperty> CreateGetStaticPropertyValueFunc<TProperty>(Type type, string propertyName)
{
var property = type.GetProperty(propertyName, typeof(TProperty));
var getPropertyValue = Expression.Property(null, property);
var lambda = Expression.Lambda<Func<TProperty>>(getPropertyValue, null);
return lambda.Compile();
}
public static Func<object> CreateGetStaticPropertyValueFunc(Type type, string propertyName)
{
var property = type.GetProperty(propertyName);
var getPropertyValue = Expression.Property(null, property);
var castPropertyValue = Expression.Convert(getPropertyValue, typeof(object));
var lambda = Expression.Lambda<Func<object>>(castPropertyValue, null);
return lambda.Compile();
}
public static Func<object> CreateGetStaticPropertyValueFunc(string typeName, string propertyName, bool isTypeFromAssembly = false)
{
Type type;
if (isTypeFromAssembly)
{
var assembly = GetAssemblyByTypeName(typeName);
type = assembly.GetType(typeName);
}
else
{
type = Type.GetType(typeName);
}
return CreateGetStaticPropertyValueFunc(type, propertyName);
}
public static Action<object, object> CreateSetPropertyValueAction(Type type, string propertyName)
{
var property = type.GetProperty(propertyName);
var target = Expression.Parameter(typeof(object));
var propertyValue = Expression.Parameter(typeof(object));
var castTarget = Expression.Convert(target, type);
var castPropertyValue = Expression.Convert(propertyValue, property.PropertyType);
var getSetMethod = property.GetSetMethod();
if (getSetMethod == null)
{
getSetMethod = property.GetSetMethod(true);
}
var call = Expression.Call(castTarget, getSetMethod, castPropertyValue);
var lambda = Expression.Lambda<Action<object, object>>(call, target, propertyValue);
return lambda.Compile();
}
public static Action<object, object> CreateSetPropertyValueAction(string typeName, string propertyName, bool isTypeFromAssembly = false)
{
Type type;
if (isTypeFromAssembly)
{
var assembly = GetAssemblyByTypeName(typeName);
type = assembly.GetType(typeName);
}
else
{
type = Type.GetType(typeName);
}
return CreateSetPropertyValueAction(type, propertyName);
}
public static Action<object, TProperty> CreateSetPropertyValueAction<TProperty>(Type type, string propertyName)
{
var property = type.GetProperty(propertyName);
var target = Expression.Parameter(typeof(object));
var propertyValue = Expression.Parameter(typeof(TProperty));
var castTarget = Expression.Convert(target, type);
var castPropertyValue = Expression.Convert(propertyValue, property.PropertyType);
var getSetMethod = property.GetSetMethod();
if (getSetMethod == null)
{
getSetMethod = property.GetSetMethod(true);
}
var call = Expression.Call(castTarget, getSetMethod, castPropertyValue);
var lambda = Expression.Lambda<Action<object, TProperty>>(call, target, propertyValue);
return lambda.Compile();
}
public static Action<object, TProperty> CreateSetPropertyValueAction<TProperty>(string typeName, string propertyName, bool isTypeFromAssembly = false)
{
Type type;
if (isTypeFromAssembly)
{
var assembly = GetAssemblyByTypeName(typeName);
type = assembly.GetType(typeName);
}
else
{
type = Type.GetType(typeName);
}
return CreateSetPropertyValueAction<TProperty>(type, propertyName);
}
public static Action<object> CreateSetStaticPropertyValueAction(Type type, string propertyName)
{
var property = type.GetProperty(propertyName);
var propertyValue = Expression.Parameter(typeof(object));
var castPropertyValue = Expression.Convert(propertyValue, property.PropertyType);
var getSetMethod = property.GetSetMethod();
if (getSetMethod == null)
{
getSetMethod = property.GetSetMethod(true);
}
var call = Expression.Call(null, getSetMethod, castPropertyValue);
var lambda = Expression.Lambda<Action<object>>(call, propertyValue);
return lambda.Compile();
}
public static Action<object> CreateSetStaticPropertyValueAction(string typeName, string propertyName, bool isTypeFromAssembly = false)
{
Type type;
if (isTypeFromAssembly)
{
var assembly = GetAssemblyByTypeName(typeName);
type = assembly.GetType(typeName);
}
else
{
type = Type.GetType(typeName);
}
return CreateSetStaticPropertyValueAction(type, propertyName);
}
public static Action<TProperty> CreateSetStaticPropertyValueAction<TProperty>(Type type, string propertyName)
{
var property = type.GetProperty(propertyName);
var propertyValue = Expression.Parameter(typeof(TProperty));
//var castPropertyValue = Expression.Convert(propertyValue, property.PropertyType);
var getSetMethod = property.GetSetMethod();
if (getSetMethod == null)
{
getSetMethod = property.GetSetMethod(true);
}
var call = Expression.Call(null, getSetMethod, propertyValue);
var lambda = Expression.Lambda<Action<TProperty>>(call, propertyValue);
return lambda.Compile();
}
public static Action<TProperty> CreateSetStaticPropertyValueAction<TProperty>(string typeName, string propertyName, bool isTypeFromAssembly = false)
{
Type type;
if (isTypeFromAssembly)
{
var assembly = GetAssemblyByTypeName(typeName);
type = assembly.GetType(typeName);
}
else
{
type = Type.GetType(typeName);
}
return CreateSetStaticPropertyValueAction<TProperty>(type, propertyName);
}
}
}
|