我有一个类,我想检查它的字段并最终报告每个字段占用的字节数.我假设所有字段都是Int32,byte等类型.
如何轻松找出该字段占用的字节数?
我需要这样的东西:
Int32 a;
// int a_size = a.GetSizeInBytes;
// a_size should be 4
Run Code Online (Sandbox Code Playgroud) 我需要能够从我的方法中读取我的属性的值,我该怎么做?
[MyAttribute("Hello World")]
public void MyMethod()
{
// Need to read the MyAttribute attribute and get its value
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个装饰器来做日志记录:
def logger(myFunc):
def new(*args, **keyargs):
print 'Entering %s.%s' % (myFunc.im_class.__name__, myFunc.__name__)
return myFunc(*args, **keyargs)
return new
class C(object):
@logger
def f():
pass
C().f()
Run Code Online (Sandbox Code Playgroud)
我想要打印:
Entering C.f
Run Code Online (Sandbox Code Playgroud)
但我收到此错误消息:
AttributeError: 'function' object has no attribute 'im_class'
Run Code Online (Sandbox Code Playgroud)
据推测,这与'logger'中'myFunc'的范围有关,但我不知道是什么.
我需要在网站或Windows应用程序中的所有程序集中查找特定类型,是否有一种简单的方法可以执行此操作?就像ASP.NET MVC的控制器工厂如何查看控制器的所有程序集一样.
谢谢.
我可以通过它的名称以编程方式获取函数代码吗?
喜欢:
function blah($a, $b) { return $a*$b; }
echo getFunctionCode("blah");
Run Code Online (Sandbox Code Playgroud)
可能吗?
是否有任何php自我描述函数来重建函数/类代码?
(意思是代替从源文件获取源代码)
在Java中存在:http://java.sun.com/developer/technicalArticles/ALT/Reflection/
在PHP中:
get_class_methods()
typeof
ReflectionFunction(感谢Alin Purcaru)
我知道Java-the-compilable-programming-language与Java-the-bytecode-format-for-JVM-execution不同.有一些例子在.class格式中有效但在.java源代码中没有,例如无构造函数的类和合成方法.
如果我们的手工工艺.class文件有保留的Java语言的关键字(例如int,while)作为类,方法或字段名,将在Java虚拟机接受它加载?
如果加载了类,是否意味着访问此类或成员的唯一方法是通过Java反射,因为该名称在Java编程语言中在语法上是非法的?
今天这真的让我很难过.我确定它不是那么难,但我有一个System.Reflection.PropertyInfo对象.我想根据数据库查找的结果设置其值(想想ORM,将列映射回属性).
我的问题是如果DB返回的值是DBNull,我只想将属性值设置为默认值,与调用相同:
value = default(T); // where T is the type of the property.
Run Code Online (Sandbox Code Playgroud)
但是,如果你给它一个Type,那么default()方法将无法编译,这就是我所拥有的:
object myObj = ???; // doesn't really matter. some arbitrary class.
PropertyInfo myPropInf = ???; // the reflection data for a property on the myObj object.
myPropInf.SetValue(myObj, default(myPropInf.PropertyType), null);
Run Code Online (Sandbox Code Playgroud)
以上不编译.默认(类型)无效.我还想过:
object myObj = ???;
PropertyInfo myPropInf = ???;
myPropInf.SetValue(myObj, Activator.CreateInstance(myPropInf.PropertyType), null);
Run Code Online (Sandbox Code Playgroud)
但是,如果Type是字符串,那将赋值"new String()",但我真的想要"null",这就是"default(string)"将返回的内容.
那我在这里错过了什么?我想一个非常hacky的方法是创建myObj的Type的新实例并复制属性,但这看起来很愚蠢......
object myObj = ???;
PropertyInfo myPropInf = ???;
var blank = Activator.CreateInstance(myObj.GetType());
object defaultValue = myPropInf.GetValue(blank, null);
myPropInf.SetValue(myObj, defaultValue, null);
Run Code Online (Sandbox Code Playgroud)
我宁愿不浪费内存来创建一个全新的实例,只是为了获得属性的默认值.似乎非常浪费. …
下面是我用来获取IsDirty检查的类中所有公共属性的初始状态的一些代码.
查看属性是否为IEnumerable的最简单方法是什么?
干杯,
Berryl
protected virtual Dictionary<string, object> _GetPropertyValues()
{
return _getPublicPropertiesWithSetters()
.ToDictionary(pi => pi.Name, pi => pi.GetValue(this, null));
}
private IEnumerable<PropertyInfo> _getPublicPropertiesWithSetters()
{
return GetType().GetProperties().Where(pi => pi.CanWrite);
}
Run Code Online (Sandbox Code Playgroud)
我最后做的是添加一些库扩展,如下所示
public static bool IsNonStringEnumerable(this PropertyInfo pi) {
return pi != null && pi.PropertyType.IsNonStringEnumerable();
}
public static bool IsNonStringEnumerable(this object instance) {
return instance != null && instance.GetType().IsNonStringEnumerable();
}
public static bool IsNonStringEnumerable(this Type type) {
if (type == null || type == typeof(string))
return false;
return typeof(IEnumerable).IsAssignableFrom(type);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试为类的静态属性执行备份/恢复功能.我可以使用反射对象getStaticProperties()方法获取所有静态属性及其值的列表.这得到两个private和public static属性及其值.
问题是我尝试使用反射对象setStaticPropertyValue($key, $value)方法恢复属性时似乎没有得到相同的结果.private并且protected变量对于此方法不可见getStaticProperties().似乎不一致.
有没有办法使用反射类或任何其他方式设置私有/受保护的静态属性?
受审
class Foo {
static public $test1 = 1;
static protected $test2 = 2;
public function test () {
echo self::$test1 . '<br>';
echo self::$test2 . '<br><br>';
}
public function change () {
self::$test1 = 3;
self::$test2 = 4;
}
}
$test = new foo();
$test->test();
// Backup
$test2 = new ReflectionObject($test);
$backup = $test2->getStaticProperties();
$test->change();
// Restore
foreach ($backup as …Run Code Online (Sandbox Code Playgroud) 我正在为WinRT应用程序编写单元测试,并且我能够使用以下方法调用非异步私有方法:
TheObjectClass theObject = new TheObjectClass();
Type objType = typeof(TheObjectClass);
objType.GetTypeInfo()
.GetDeclaredMethod("ThePrivateMethod")
.Invoke(theObject, null);
Run Code Online (Sandbox Code Playgroud)
但是,如果有问题的私有方法是async,代码将继续执行而不等待它完成.
如何await为此添加功能?
reflection ×10
c# ×6
.net ×3
php ×2
.class-file ×1
asp.net ×1
async-await ×1
attributes ×1
byte ×1
java ×1
jvm ×1
methods ×1
python ×1
types ×1
visibility ×1