这是上下文:
我尝试编写一个映射器,用于动态地将我的DomainModel对象转换为ViewModel Ojects.我得到的问题是,当我尝试通过反射调用泛型类的方法时,我得到了这个错误:
System.InvalidOperationException:无法对ContainsGenericParameters为true的类型或方法执行后期绑定操作.
有人可以帮助我找出错误在哪里吗?这将不胜感激
这是代码(我试图简化它):
public class MapClass<SourceType, DestinationType>
{
public string Test()
{
return test
}
public void MapClassReflection(SourceType source, ref DestinationType destination)
{
Type sourceType = source.GetType();
Type destinationType = destination.GetType();
foreach (PropertyInfo sourceProperty in sourceType.GetProperties())
{
string destinationPropertyName = LookupForPropertyInDestinationType(sourceProperty.Name, destinationType);
if (destinationPropertyName != null)
{
PropertyInfo destinationProperty = destinationType.GetProperty(destinationPropertyName);
if (destinationProperty.PropertyType == sourceProperty.PropertyType)
{
destinationProperty.SetValue(destination, sourceProperty.GetValue(source, null), null);
}
else
{
Type d1 = typeof(MapClass<,>);
Type[] typeArgs = { destinationProperty.GetType(), sourceType.GetType() };
Type constructed = …Run Code Online (Sandbox Code Playgroud) 在单元测试中,我需要测试alias_method定义的别名方法是否已正确定义.我可以简单地对用于原始的别名使用相同的测试,但我想知道是否有更明确或有效的解决方案.例如,有没有办法1)取消引用方法别名并返回其原始名称,2)获取并比较某种基础方法标识符或地址,或3)获取和比较方法定义?例如:
class MyClass
def foo
# do something
end
alias_method :bar, :foo
end
describe MyClass do
it "method bar should be an alias for method foo" do
m = MyClass.new
# ??? identity(m.bar).should == identity(m.foo) ???
end
end
Run Code Online (Sandbox Code Playgroud)
建议?
我有一些代码(工作正常)看起来像这样:
int integer = 42;
decimal? castTo = integer;
Run Code Online (Sandbox Code Playgroud)
然后我想用反射做类似的事情,有些代码看起来像这样:
object value = source; // source was an int originally
var parameters = new object[1];
...
parameters[0] = value;
var setMethod = property.GetSetMethod();
// Call the set method, which takes a decimal? as a parameter
setMethod.Invoke(o, parameters);
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我得到:
failed: System.ArgumentException : Object of type 'System.Int32' cannot be converted to type 'System.Nullable`1[System.Decimal]'.
at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature …Run Code Online (Sandbox Code Playgroud) 我在winform上有一个命令按钮.所以,如果我有类似的东西:
myButton.Click += MyHandler1;
myButton.Click += MyHandler2;
myButton.Click += MyHandler3;
Run Code Online (Sandbox Code Playgroud)
如何判断是否已将任何特定的MyHandler添加到Click事件中,以便它不会再添加到我的代码中的其他位置?
我已经阅读了如何使用GetInvocationList()来获取自己的事件信息.但是在尝试使用各种组合获取命令按钮的项目时出错.它说,
"事件'System.Windows.Forms.Control.Click'只能出现在+ =或 - =的左侧."
我错过了什么?
[编辑] - 我想强调艾哈迈德指出的这个问题.这是一个kludge,应该更容易恕我直言,但看起来它可能只是工作.
期望的输出示例:
(Lorg/w3c/dom/Node;)Lorg/w3c/dom/Node;
(Ljava/lang/String;)Lorg/w3c/dom/Attr;
Run Code Online (Sandbox Code Playgroud)
可以使用javap实用程序生成此类签名:
javap -s -p org.w3c.dom.Node
Run Code Online (Sandbox Code Playgroud)
但有没有办法以编程方式生成它们.我不想手动编写jni规范中的所有规则.
我在做一个项目,在那里我需要注册的所有属性,因为系统是如此巨大,将需要大量的工作来注册的一切,我希望依赖XAML的目的属性.
目标是找到树顶部的所有属性.
所以基本上
public class A{
public int Property1 { get; set; }
}
public class B : A{
public int Property2 { get; set; }
public virtual int Property3 { get; set; }
}
public class C : B{
public override int Property3 { get; set; }
public int Property4 { get; set; }
public int Property5 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
最终结果将是这样的
A.Property1
B.Property2
B.Property3
C.Property4
C.Property5
Run Code Online (Sandbox Code Playgroud)
如果您注意到我不想接受被覆盖的属性,因为我搜索属性的方式如果我做这样的事情
C.Property3例如,它找不到它会检查C的基本类型,它会找到它.
这就是我到目前为止所拥有的.
public static void RegisterType( Type type )
{ …Run Code Online (Sandbox Code Playgroud) 如果我com.example.test.Enum2.Test在下面的代码中有一个类,为什么getCanonicalName()返回com.example.test.Enum2.Test但Class.forName()需要"com.example.test.Enum2$Test"作为参数?
有没有办法保持一致,这样我可以通过名称序列化/反序列化枚举值,而不必检查每个$vs .可能性,当枚举是一个嵌套类?
package com.example.test;
import java.util.Arrays;
public class Enum2 {
enum Test {
FOO, BAR, BAZ;
}
public static void main(String[] args) {
for (String className : Arrays.asList(
"com.example.test.Enum2.Test",
"com.example.test.Enum2$Test"))
{
try {
Class<?> cl = Class.forName(className);
System.out.println(className+" found: "+cl.getCanonicalName());
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
System.out.println(Test.FOO.getDeclaringClass().getCanonicalName());
}
}
Run Code Online (Sandbox Code Playgroud)
澄清:我正在寻找一种在实际应用中处理这个问题的好方法(不仅仅是上面提到的测试用例),或者:
一个.使用getCanonicalName()输出序列化/反序列化(仅点缀名称),并Class.forName()依次尝试每种可能性,例如"com.example.test.Enum2.Test",然后"com.example.test.Enum2$Test",然后"com.example.test$Enum2$Test",等等.
湾 使用正确的$表示法,这样 …
我知道默认情况下Java没有所谓的eval(我称之为"邪恶")方法.这听起来像是一件坏事 - 知道你没有其他许多人做的事情.但更糟糕的是,似乎被告知你无法拥有它.
我的问题是:背后有什么可靠的推理?我的意思是,谷歌这个只会返回大量的旧数据和虚假的原因 - 即使有一个我正在寻找的答案,我也无法从那些只是抛出通用标签词的人那里过滤掉它.
我对那些告诉我如何解决问题的答案不感兴趣; 我自己可以这样做:
文件sample.py(py文件夹中)内容:
def factorial(n):
return reduce(lambda x, y:x * y, range(1, n + 1))
Run Code Online (Sandbox Code Playgroud)
和Java代码:
ScriptEngine engine = new ScriptEngineManager().getEngineByName("jython");
engine.eval(new FileReader("py" + java.io.File.separator + "sample.py"));
System.out.println(engine.eval("factorial(932)"));
Run Code Online (Sandbox Code Playgroud)

这相当于:
String expr = "N[Integrate[E^(2 y^5)/(2 x^3), {x, 4, 7}, {y, 2, 3}]]";
System.out.println(MM.Eval(expr));
//Output: 1.5187560850359461*^206 + 4.2210685420287355*^190*I
Run Code Online (Sandbox Code Playgroud)
Runtime.getRuntime().exec以root身份使用我有一个看起来像这样的结构:
public struct MyStruct
{
public const string Property1 = "blah blah blah";
public const string Property2 = "foo";
public const string Property3 = "bar";
}
Run Code Online (Sandbox Code Playgroud)
我想以编程方式检索MyStruct的const属性值的集合.到目前为止,我已经尝试过这个并没有成功:
var x = from d in typeof(MyStruct).GetProperties()
select d.GetConstantValue();
Run Code Online (Sandbox Code Playgroud)
有人有主意吗?谢谢.
编辑:这是最终为我工作的:
from d in typeof(MyStruct).GetFields()
select d.GetValue(new MyStruct());
Run Code Online (Sandbox Code Playgroud)
感谢Jonathan Henson和JaredPar的所有帮助!
我在命名空间Domain中有B类及其父类A.
然后我在名称空间Reflect中有一个Reflection Util.如果我使用这一行
instanceOfB.GetType().GetFields(BindingFlags.NonPublic
| BindingFlags.Public | BindingFlags.Instance );
Run Code Online (Sandbox Code Playgroud)
要查找所有字段(a和b),我只得到b.但是,当我a受到保护或公开时,我也会发现它们.
我还需要做些什么才能找到基类的私有字段?
reflection ×10
c# ×6
java ×3
.net ×2
algorithm ×1
alias-method ×1
asp.net-mvc ×1
delegates ×1
generics ×1
invocation ×1
nested-class ×1
propertyinfo ×1
ruby ×1
types ×1
visibility ×1
winforms ×1