目前已在F#中添加了对反射的支持,但它不适用于度量类型.是否可以在F#中使用反射来测量类型?我读过这个.它是在2008年,但是如果你在ildasm中查看一些像bellow这样的代码你就看不出任何东西了Units of Measure.
// Learn more about F# at http://fsharp.net
[<Measure>] type m
[<Measure>] type cm
let CalculateVelocity(length:float<m> ,time:float<cm>) =
length / time
Run Code Online (Sandbox Code Playgroud)
ildasm输出:
.method public static float64 CalculateVelocity(float64 length,
float64 time) cil managed
{
// Code size 5 (0x5)
.maxstack 4
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldarg.1
IL_0003: div
IL_0004: ret
} // end of method Program::CalculateVelocity
Run Code Online (Sandbox Code Playgroud)
所以有些东西无法在F#中反映出来.这是真的吗?看到评论:CLR中的单位实际上根本没有被看到......在文章中.
我有一个扩展方法:
public static class StringEx
{
public static bool Like(this string a, string b)
{
return a.ToLower().Contains(b.ToLower());
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用我的参数通过GetMethod正确反映它?我试过这个没有成功(静态方法有例外):
var like = typeof(StringEx).GetMethod("Like", new[] {typeof(string), typeof(string)});
comparer = Expression.Call(prop, like, value);
Run Code Online (Sandbox Code Playgroud) 我使用Reflections库开发了一个应用程序,用于查询具有特定注释的所有类.在我决定从我的应用程序创建一个Eclipse插件之前,一切都像魅力一样.然后,思考停止工作.
鉴于我的应用程序在不是Eclipse插件的一部分时工作正常,我认为它应该是一个类加载器问题.所以我在我Reflections的类中添加了插件激活器类的类加载器,上下文类加载器以及我能想象的所有其他类加载器,没有任何成功.这是我的代码的简化版本:
ConfigurationBuilder config = new ConfigurationBuilder();
config.addClassLoaders(thePluginActivatorClassLoader);
config.addClassLoaders(ClasspathHelper.getContextClassLoader());
config.addClassLoaders("all the classloaders I could imagine");
config.filterInputsBy(new FilterBuilder().include("package I want to analyze"));
Reflections reflections = new Reflections(config);
Set<Class<?>> classes = reflections.getTypesAnnotatedWith(MyAnnotation.class); //this Set is empty
Run Code Online (Sandbox Code Playgroud)
我也尝试添加我想要加载到ConfigurationBuilder类的类的URL ,但它没有帮助.
有人能告诉我是否有办法让Reflections工作成为Eclipse插件的一部分?或者我应该更好地寻找另一种替代方案?非常感谢,我真的很困惑.
主要目标是使用我自己的实现覆盖Android系统类(Activity,View等).
http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html
实现了自定义类加载的ClassLoader,加载非系统类(自定义类).
但是当我尝试使用我的实现加载Activity时 - 它没有加载,因为ClassLoader已经在其缓存中有这个类:
/**
* Returns the class with the specified name if it has already been loaded
* by the virtual machine or {@code null} if it has not yet been loaded.
*
* @param className
* the name of the class to look for.
* @return the {@code Class} object or {@code null} if the requested class
* has not been loaded.
*/
protected final Class<?> findLoadedClass(String className) {
ClassLoader loader;
if (this == BootClassLoader.getInstance()) …Run Code Online (Sandbox Code Playgroud) 我有一节课:
class MyClass {
private var num : Int;
}
Run Code Online (Sandbox Code Playgroud)
我想知道该字段具有类型,Int 而不管当前值是什么null,例如.
我正在寻找替代方法以类型安全的方式获取属性名称.
例如,使用lambdaj,我可以这样做:
propertyName(on(Contract.class).getContractKey())
Run Code Online (Sandbox Code Playgroud)
但这有点冗长,性能也不是很好.
我希望在构建期间生成一些东西.
我有一个XML文件,我有一个节点,我读了所有ChildNodes.childNode的名称与我必须使用此childNode的值设置的变量匹配.
在循环中,我想设置:
C#代码:
protected string myvar1;
protected string myvar2;
Run Code Online (Sandbox Code Playgroud)
XML内容如下所示:
<parameters>
<myvar1>MyValue1</myvar1>
<myvar2>MyValue2</myvar2>
</parameters>
Run Code Online (Sandbox Code Playgroud)
C#设置变量:
foreach (var item in xmlParamInstallation.SelectNodes("parameters")[0].ChildNodes)
{
??????
}
Run Code Online (Sandbox Code Playgroud)
任何的想法 ?
谢谢,
更新1: 循环中的值"field"始终为null.
public class ParametersTest
{
public string myvar1 { get; set; }
public string myvar2 {get; set;}
}
var type = typeof(ParametersTest);
foreach (XmlNode item in xmlParamInstallation.SelectNodes("parameters")[0].ChildNodes)
{
var field = type.GetField(item.LocalName);
field.SetValue(field, item.InnerText);
}
Run Code Online (Sandbox Code Playgroud) 在C/C++中,我经常发现它在调试时有用,比如定义一个宏,ECHO(x)打印出变量名及其值(即ECHO(variable)可能打印variable 7).您可以使用"字符串化操作符的宏来得到变量名#描述这里.有没有办法在Python中这样做?
换句话说,我想要一个功能
def echo(x):
#magic goes here
Run Code Online (Sandbox Code Playgroud)
如果被称为foo=7; echo(foo)(或foo=7; echo('foo')可能),将打印出来foo 7.我意识到如果我将变量及其名称都传递给函数,这样做是微不足道的,但是在调试时我会使用这样的函数,重复总是会让我感到恼火.
我最近阅读了关于属性和反射的内容,我认为在我的程序中包含元数据是一种很好的方法.我有这个抽象类,我希望从它继承的所有类都向类声明一些属性,因为我希望为我的程序创建自定义组件(那些派生类),并希望在运行时读取这些类的元数据.但是,派生类都必须显式声明我存储元数据的属性.那么如何在派生类中强制执行属性声明?谢谢.
有没有办法通过字符串(名称)访问成员?
例如,如果静态代码是:
classA.x = someFunction(classB.y);
Run Code Online (Sandbox Code Playgroud)
但我只有两个字符串:
string x = "x";
string y = "y";
Run Code Online (Sandbox Code Playgroud)
我知道在JavaScript中你可以做到:
classA[x] = someFunction(classB[y]);
Run Code Online (Sandbox Code Playgroud)
但是如何在C#中做到这一点?
此外,是否可以按字符串定义名称?
例如:
string x = "xxx";
class{
bool x {get;set} => means bool xxx {get;set}, since x is a string
}
Run Code Online (Sandbox Code Playgroud)
更新,对于tvanfosson,我无法让它工作,它是:
public class classA
{
public string A { get; set; }
}
public class classB
{
public int B { get; set; }
}
var propertyB = classB.GetType().GetProperty("B");
var propertyA = classA.GetType().GetProperty("A");
propertyA.SetValue( classA, someFunction( propertyB.GetValue(classB, null) as …Run Code Online (Sandbox Code Playgroud) reflection ×10
c# ×4
java ×3
classloader ×2
.net ×1
android ×1
attributes ×1
class ×1
eclipse ×1
f# ×1
get-method ×1
haxe ×1
inheritance ×1
lambdaj ×1
python ×1
static ×1
variables ×1
xml ×1