使用INotifyPropertyChanged时指定属性名称的最佳方法是什么?
大多数示例将属性名称硬编码为PropertyChanged事件的参数.我在考虑使用MethodBase.GetCurrentMethod.Name.Substring(4),但对反射开销有点不安.
在我目前的项目中,我觉得有必要使用反射在Java中创建一种模拟回调系统.但是,我在解决实际功能方面遇到了问题.有问题的代码如下:
public Callback(Object parentObj, String methodName, Class<?>...parameters)
{
if(parentObj == null)
throw new IllegalArgumentException("parentObj cannot be null", new NullPointerException());
Class<?> clazz = parentObj.getClass();
// Trace debugging, see output
for(Method m : clazz.getDeclaredMethods())
if(m.getName().equals("myMethod")) System.out.println (m);
try { this.method = clazz.getMethod(methodName, parameters); }
catch(NoSuchMethodException nsme) { nsme.printStackTrace(); } // Exception caught
catch(SecurityException se) { se.printStackTrace(); }
this.parentObj = parentObj;
this.parameters = parameters;
}
Run Code Online (Sandbox Code Playgroud)
当我构造Callback对象时,我使用这样的语法:
new Callback(this, "myMethod", boolean.class)
Run Code Online (Sandbox Code Playgroud)
当我尝试创建我的伪回调时,它会命中NoSuchMethodExceptioncatch块.我在上面包含了一些跟踪调试,以显示我的一个方法的输出失败.输出:
private void my.package.MyClass.myMethod(boolean)
java.lang.NoSuchMethodException: my.package.MyClass.myMethod(boolean)
at java.lang.Class.getMethod(Class.java:1605)
at …Run Code Online (Sandbox Code Playgroud) 对不起,我正在查找文档中的System.Type类型和PropertyInfo类型,但我似乎无法找到我需要的东西.
如何判断属性(或方法或任何其他成员)是否virtual在其声明类中声明?
例如
class Cat
{
public string Name { get; set; }
public virtual int Age { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如何判断Age财产是否被宣布virtual?
例如:
public class Test {
public static void main(String[] args) throws Exception {
Car c= (Car) Class.forName("Car").newInstance();
System.out.println(c.getName());
}
}
class Car {
String name = "Default Car";
String getName(){return this.name;}
}
Run Code Online (Sandbox Code Playgroud)
明确的代码.
但是,如果我用params添加构造函数,有些像这样:
public Car(String name)
{this.name = name;}
Run Code Online (Sandbox Code Playgroud)
我知道了: java.lang.InstantiationException
所以,不,我不知道,如何通过params传递构造函数.
请帮忙.
在集合方面,我理解泛型.但是在Class<T>课堂上它意味着什么呢?实例化Class对象时,只有一个对象.那为什么T参数呢?这是什么规定的?为什么有必要(如果是)?
我试图在一个简单的静态类中循环一些静态属性,以便用它们的值填充组合框,但是遇到了困难.
这是简单的类:
public static MyStaticClass()
{
public static string property1 = "NumberOne";
public static string property2 = "NumberTwo";
public static string property3 = "NumberThree";
}
Run Code Online (Sandbox Code Playgroud)
...以及试图检索值的代码:
Type myType = typeof(MyStaticClass);
PropertyInfo[] properties = myType.GetProperties(
BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
foreach (PropertyInfo property in properties)
{
MyComboBox.Items.Add(property.GetValue(myType, null).ToString());
}
Run Code Online (Sandbox Code Playgroud)
如果我不提供任何绑定标志,那么我得到大约57个属性,包括System.Reflection.Module模块和我不关心的各种其他继承的东西.我的3个声明的属性不存在.
如果我提供其他标志的各种组合,那么它总是返回0属性.大.
我的静态类是否真的在另一个非静态类中声明是否重要?
我究竟做错了什么?
在我读过的所有书中,他们经常说,你想要动态生成IL的情况并不多,但他们没有给出任何有意义的例子.
在看到Reflection.Emit作为游戏公司的工作要求后,我很好奇其他地方正在使用它.
我现在想知道在现实世界中是否有任何情况,这是解决问题的最佳方法.也许它被用作设计模式的实现?
注意 我想PostSharp/AOP使用它.
我想做这样的事情:
public MyFunction(int integerParameter, string stringParameter){
//Do this:
LogParameters();
//Instead of this:
//Log.Debug("integerParameter: " + integerParameter +
// ", stringParameter: " + stringParameter);
}
public LogParameters(){
//Look up 1 level in the call stack (if possible),
//Programmatically loop through the function's parameters/values
//and log them to a file (with the function name as well).
//If I can pass a MethodInfo instead of analyzing the call stack, great.
}
Run Code Online (Sandbox Code Playgroud)
我甚至不确定我想要做什么是可能的,但能够在运行时自动将参数名称/值输出到文件而不显式编写代码来记录它们将是非常好的.
可能吗?
我有一个枚举,每个成员都有一个自定义属性应用于它.如何检索存储在每个属性中的值?
现在我这样做:
var attributes = typeof ( EffectType ).GetCustomAttributes ( false );
foreach ( object attribute in attributes )
{
GPUShaderAttribute attr = ( GPUShaderAttribute ) attribute;
if ( attr != null )
return attr.GPUShader;
}
return 0;
Run Code Online (Sandbox Code Playgroud)
另一个问题是,如果找不到,我应该返回什么?0是可以转换为任何枚举,对吧?这就是我回归的原因.
忘记提及,上面的代码为每个枚举成员返回0.
reflection ×10
.net ×5
c# ×5
class ×3
java ×3
generics ×1
logging ×1
parameters ×1
properties ×1
static ×1
wpf ×1