在派生类中声明与基类中的属性名称匹配的属性"隐藏"它(除非它使用override关键字覆盖它).Type.GetProperties()如果它们的类型不匹配,则返回基类和派生类属性.但是,如果它们的类型不匹配,令人震惊的是仅返回派生类的属性.例如:
class A
{
protected double p;
public int P { get { return (int)p; } set { p = value; } }
}
class B : A
{
public new int P { get { return (int)p; } set { p = value; } }
}
class C : B
{
public new float P { get { return (float)p; } set { p = value; } }
}
Run Code Online (Sandbox Code Playgroud)
呼叫typeof(C).GetProperties()只会返回BP和CP是否可以以GetProperties() …
可能重复:
查找传递给C#中函数的变量名称
在C#中,有没有办法(更好的方法)在运行时解析参数的名称?
例如,在以下方法中,如果重命名方法参数,则还必须记住更新传递给ArgumentNullException的字符串文字.
public void Woof(object resource)
{
if (resource == null)
{
throw new ArgumentNullException("resource");
}
// ..
}
Run Code Online (Sandbox Code Playgroud) 我有一个程序集(作为ReflectionOnly加载),我想找到这个程序集中的所有命名空间,所以我可以将它们转换为自动生成的源代码文件模板的"using"(VB中的"Imports")语句.
理想情况下,我只想将自己限制在顶级命名空间,而不是:
using System;
using System.Collections;
using System.Collections.Generic;
Run Code Online (Sandbox Code Playgroud)
你只会得到:
using System;
Run Code Online (Sandbox Code Playgroud)
我注意到System.Type类上有一个Namespace属性,但有没有更好的方法来收集程序集内的Namespaces,它不涉及迭代所有类型并剔除重复的命名空间字符串?
大卫,很有责任
我是Java的新手,我正面临一个反思问题.
假设我必须fooMethod在类的实例上动态调用该方法Foobar
到目前为止,我得到了一个例子Foobar:
Object instance = Class.forName("Foobar").newInstance();
Run Code Online (Sandbox Code Playgroud)
假设我知道fooMethod这个对象有一个方法(我甚Class.forName("Foobar").getDeclaredMethods()至可以查看),请问如何调用它?
class A {
public static void foo() {}
}
class B {
public static void foo() {}
}
Run Code Online (Sandbox Code Playgroud)
我有Class clazz = A.class; or B.class;
我如何通过"clazz"访问它,假设它可能被指定为'A'或'B'
我有一个泛型类和一个派生类如下.
public class GenericClass<T> { ... }
public class DerivedClass : GenericClass<SomeType> { ... }
Run Code Online (Sandbox Code Playgroud)
如何通过反射找到派生类?我在下面尝试了两种方法,但似乎没有用.
System.Reflection.Assembly.GetExecutingAssembly().GetTypes().Where(t => typeof(GenericClass<>).IsAssignableFrom(t));
System.Reflection.Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsSubclassOf(typeof(GenericClass<>));
Run Code Online (Sandbox Code Playgroud) 我有以下方法应该检索加载的本地(在bin文件夹)程序集的列表:
static IEnumerable<Assembly> GetLocalAssemblies()
{
Assembly callingAssembly = Assembly.GetCallingAssembly();
string path = new Uri(Path.GetDirectoryName(callingAssembly.CodeBase)).AbsolutePath;
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
return assemblies.Where(x => !x.IsDynamic && new Uri(x.CodeBase).AbsolutePath.Contains(path)).ToList();
}
Run Code Online (Sandbox Code Playgroud)
但是,程序集列表缺少我需要它的几个程序集.我需要的程序集是管理的(c#.net 4),在项目中引用,并存在于bin文件夹中.
为什么bin文件夹中存在的二进制文件在应用程序启动时不会进入AppDomain?
给定类值:
public class Value {
private int xVal1;
private int xVal2;
private double pVal;
// constructor of the Value class
public Value(int _xVal1 ,int _xVal2 , double _pVal)
{
this.xVal1 = _xVal1;
this.xVal2 = _xVal2;
this.pVal = _pVal;
}
public int getX1val()
{
return this.xVal1;
}
...
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用以下方法创建该类的新实例reflection:
来自Main:
.... // some code
....
....
int _xval1 = Integer.parseInt(getCharacterDataFromElement(line));
int _xval2 = Integer.parseInt(getCharacterDataFromElement(line2));
double _pval = Double.parseDouble(getCharacterDataFromElement(line3));
Class c = null;
c = Class.forName("Value");
Object o = …Run Code Online (Sandbox Code Playgroud) class Bla
{
public readonly int sum;
}
FieldInfo f = type.GetField("sum");
f.?? // what?
Run Code Online (Sandbox Code Playgroud)
如何查找是否sum只读?对于属性,我可以PropertyInfo.CanWrite查找成员是否具有写访问权限.
我一直试图理解使用new实例化对象与使用之间的区别Class.forName("A").newInstance();.
我已经为一个简单的类运行了以下代码,该类A显示使用Class.forname("A").newInstance()比使用just的速度慢70-100倍new A().
我很想知道为什么会有这么大的时间差异,但无法弄清楚.请有人帮我理解原因.
public class Main4test {
public Main4test() {
}
static int turns = 9999999;
public static void main(String[] args) {
new Main4test().run();
}
public void run() {
System.out.println("method1: " + method1() + "");
System.out.println("method2:" + method2() + "");
}
public long method2() {
long t = System.currentTimeMillis();
for (int i = 0; i < turns; i++) {
try {
A a = (A) Class.forName("A").newInstance();
} catch (InstantiationException …Run Code Online (Sandbox Code Playgroud) reflection ×10
c# ×6
java ×4
.net ×2
assemblies ×2
constructor ×1
fieldinfo ×1
inheritance ×1
namespaces ×1
readonly ×1
vb.net ×1