统一java库中C#的反思

时间:2015-10-13 15:38:00

标签: java c# ikvm

我已经使用IKVM将java库转换为.dll以在我的Unity应用程序中使用。

我如何在c#中编写这个java行:

ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);


org.gephi.project.api.ProjectController pc = org.openide.util.Lookup.getDefault().lookup(org.gephi.project.api.ProjectController.class);

反省似乎不起作用。当我引用ProjectController.class

时似乎有问题

1 个答案:

答案 0 :(得分:0)

.NET中运行时Java类的等价物是System.Type对象。有两种方法可以获得一个:在任何对象实例上调用GetType()(来自System.Object,所以一切都支持它)或使用the typeof operator

可能也只能使用原始类名,但我希望它能够使用Type对象。这取决于IKVM如何实现Java反射;我真的不知道。

当然,您也可以使用.NET反射来完成整个过程。我不会知道这是否与您的示例行相同,因为您使用了一些奇怪的非标准Java反射式的东西,但它看起来很像像你一样只是试图获得"默认"一个班级的实例......?

// I haven't tested this, but it may work:
ProjectController pc = Lookup.getDefault().lookup(typeof(ProjectController));
// You could also just try it without the ".class" part (since "class" is reserved in C#):
ProjectController pc = Lookup.getDefault().lookup(ProjectController);
// Or, if you want the .NET reflection "proper" way to do what I *think* you're attempting:
ProjectController pc = typeof(ProjectController).GetContructor(Type.EmptyTypes).Invoke(null);
// Of course, that's just the same thing as this simple line, so I probably misunderstood:
ProjectController pc = new ProjectController();