|
結局 Reflection の嵐になってしまいました。
2.0 と 3.5 は情報が埋め込まれてないようです。
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
static class Program
{
[STAThread]
static void Main() {
var asmHelper = new ReflectionHelper(Assembly.GetExecutingAssembly());
var attributes = asmHelper.Invoke("GetCustomAttributesData");
if (attributes == null) {
Debug.Print(".NET Framework 2.0/3.5");
} else {
foreach (var attr in (IEnumerable)attributes) {
var attrHelper = new ReflectionHelper(attr);
var attributeType = new ReflectionHelper(attrHelper.GetProperty("AttributeType"));
var fullName = (string)attributeType.GetProperty("FullName");
if (fullName == "System.Runtime.Versioning.TargetFrameworkAttribute") {
var namedArguments = new ReflectionHelper(attrHelper.GetProperty("NamedArguments"));
var item = new ReflectionHelper(namedArguments.GetProperty("Item", 0));
var typedValue = new ReflectionHelper(item.GetProperty("TypedValue"));
Debug.Print((string)typedValue.GetProperty("Value"));
}
}
}
}
private class ReflectionHelper
{
private object Target;
private Type TargetType;
public ReflectionHelper(object target) {
Target = target;
TargetType = target.GetType();
}
public object GetProperty(string propertyName, params object[] parameters) {
var pi = TargetType.GetProperty(propertyName);
if (pi != null) {
return pi.GetValue(Target, parameters);
}
return null;
}
public object Invoke(string methodName, params object[] parameters) {
var mi = TargetType.GetMethod(methodName);
if (mi != null) {
return mi.Invoke(Target, parameters);
}
return null;
}
}
}
|