如何获取实例成员的值?
对于propertyInfos,有一个propertyInfo.GetValue(instance, index),但memberInfo中不存在这样的东西.
我在网上搜索,但似乎停止获取会员的姓名和类型.
我有我的winform应用程序使用数据绑定收集数据.一切都很好,除了我必须使用字符串链接属性与textedit:
Me.TextEdit4.DataBindings.Add(New System.Windows.Forms.Binding("EditValue",Me.MyClassBindingSource,"MyClassProperty",True))
这工作正常,但如果我更改类的属性名称,编译器显然不会警告我.
我希望能够通过反射获取属性名称,但我不知道如何指定我想要的属性的名称(我只知道如何在类的所有属性之间进行迭代)
任何的想法?
场景:
我正在编写一个层来将3个类似的Web服务抽象为一个可用的类.每个Web服务都公开一组共享共性的对象.我创建了一组利用共性的中间对象.但是在我的图层中,我需要在Web服务对象和我的对象之间进行转换.
在调用Web服务之前,我已经使用反射在运行时创建了相应的类型,如下所示:
public static object[] CreateProperties(Type type, IProperty[] properties)
{
//Empty so return null
if (properties==null || properties.Length == 0)
return null;
//Check the type is allowed
CheckPropertyTypes("CreateProperties(Type,IProperty[])",type);
//Convert the array of intermediary IProperty objects into
// the passed service type e.g. Service1.Property
object[] result = new object[properties.Length];
for (int i = 0; i < properties.Length; i++)
{
IProperty fromProp = properties[i];
object toProp = ReflectionUtility.CreateInstance(type, null);
ServiceUtils.CopyProperties(fromProp, toProp);
result[i] = toProp;
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
这是我的调用代码,来自我的一个服务实现:
Property[] props …Run Code Online (Sandbox Code Playgroud) 对某些C#代码有一个奇怪的问题 - 当没有明确标记时,属性的Getter方法显示为虚拟.
该问题在此类上显示DbKey属性(代码完整):
public class ProcessingContextKey : BusinessEntityKey, IProcessingContextKey
{
public ProcessingContextKey()
{
// Nothing
}
public ProcessingContextKey(int dbKey)
{
this.mDbKey = dbKey;
}
public int DbKey
{
get { return this.mDbKey; }
set { this.mDbKey = value; }
}
private int mDbKey;
public override Type GetEntityType()
{
return typeof(IProcessingContextEntity);
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用反射来检查DbKey属性时,我得到以下(意外)结果:
Type t = typeof(ProcessingContextKey);
PropertyInfo p = t.GetProperty("DbKey");
bool virtualGetter = p.GetGetMethod(true).IsVirtual; // True!
bool virtualSetter = p.GetSetMethod(true).IsVirtual; // False
Run Code Online (Sandbox Code Playgroud)
为什么virtualGetter设置为True?鉴于该属性既不是抽象的也不是虚拟的,我期望是假的.
为了完整性 …
有没有办法从特定的命名空间中获取对象?或许与System.Reflections?我希望将所有对象从ITestType命名空间中的类型Test.TestTypes作为对象获取,以便我有一个实例列表,TestType1, TestType2, TestType3依此类推.有人能帮我吗?我不知道在哪里搜索.
我目前正在使用用Java实现的专门的,解释性的编程语言.作为该语言的一小部分,我想添加调用Java的能力.在深入研究反射的所有细节之前,我想知道是否有人知道一个通用库来执行反向调用Java代码的"后端"部分.
也就是说,我将一个字符串(我定义语法)解析为一些表示Java方法调用(或构造函数或字段访问)的数据结构,然后将该数据结构传递给调用该调用并返回结果的库.特别是,我希望它已经处理了我不想弄清楚的所有边缘情况:
我花了一点时间在JVM上查看动态语言的实现,但这些通常比我正在寻找的要复杂得多,或者针对特定语言进行了高度优化.
另一个选择是将我的语法转换为某种动态语言的字符串,并使用Rhino或其他东西调用它,但这比我正在寻找的开销稍微多一些.
谢谢!
我正在尝试检测Type对象的特定实例是否是通用的"IEnumerable"...
我能想到的最好的是:
// theType might be typeof(IEnumerable<string>) for example... or it might not
bool isGenericEnumerable = theType.GetGenericTypeDefinition() == typeof(IEnumerable<object>).GetGenericTypeDefinition()
if(isGenericEnumerable)
{
Type enumType = theType.GetGenericArguments()[0];
etc. ...// enumType is now typeof(string)
Run Code Online (Sandbox Code Playgroud)
但这似乎有点间接 - 是否有更直接/更优雅的方式来做到这一点?
class Foo { }
class Foo1 : Foo { }
class Foo2 : Foo { }
Run Code Online (Sandbox Code Playgroud)
我怎样才能将所有使用Foo的类作为基类?在同一程序集中不需要继承的类.
可以说我有一个java包commands,其中包含所有继承自的类,ICommand我能以某种方式得到所有这些类吗?我正在锁定以下内容:
Package p = Package.getPackage("commands");
Class<ICommand>[] c = p.getAllPackagedClasses(); //not real
Run Code Online (Sandbox Code Playgroud)
有可能吗?
是否可以以这样的方式键入字符串数组,使得该数组只能是给定对象中的有效属性路径?类型定义应该适用于所有深度嵌套的对象。
例子:
const object1 = {
someProperty: true
};
const object2 = {
nestedObject: object1,
anotherProperty: 2
};
type PropertyPath<Type extends object> = [keyof Type, ...Array<string>]; // <-- this needs to be improved
// ----------------------------------------------------------------
let propertyPath1: PropertyPath<typeof object1>;
propertyPath1 = ["someProperty"]; // works
propertyPath1 = ["doesntExist"]; // should not work
let propertyPath2: PropertyPath<typeof object2>;
propertyPath2 = ["nestedObject", "someProperty"]; // works
propertyPath2 = ["nestedObject", "doesntExist"]; // should not work
propertyPath2 = ["doesntExist"]; // should not work
Run Code Online (Sandbox Code Playgroud)
reflection ×10
c# ×5
.net ×4
java ×2
.net-3.5 ×1
arrays ×1
assemblies ×1
casting ×1
dynamic ×1
generics ×1
inheritance ×1
invoke ×1
jsonpointer ×1
namespaces ×1
package ×1
recursion ×1
types ×1
typescript ×1