我正在使用构建器模式来分离一堆不同的配置可能性.基本上,我有一堆名为ID的类(类似于ID12345).这些都继承自基础构建器类.在我的脚本中,每次运行此应用程序时,我都需要为每个类(大约50个)实例化一个实例.所以,我试图看看是否代替做这样的事情:
ProcessDirector = ProcessDirector()
ID12345 = ID12345()
ID01234 = ID01234()
ProcessDirector.construct(ID12345)
ProcessDirector.construct(ID01234)
ID12345.run()
ID01234.run()
Run Code Online (Sandbox Code Playgroud)
我可以做这样的事情(我知道这不起作用):
IDS = ["ID12345", "ID01234"]
ProcessDirector = ProcessDirector()
for id in IDS:
builder = id() #some how instantiate class from string
ProcessDirector.construct(builder)
builder.run()
Run Code Online (Sandbox Code Playgroud)
这样,当我需要在将来添加一个新的时,我所要做的就是将ID添加到IDS列表中,而不是在整个代码中添加新的ID.
看起来根据数据的来源,有一些不同的意见.这些ID输入到其他人无法访问的文件中.我不是从命令行读取字符串,并且我希望将来在添加新ID时能够进行少量更改.
我有一节课:
class A {
public string a = "A-val" , b = "B-val";
}
Run Code Online (Sandbox Code Playgroud)
我想通过反射打印对象成员
//Object here is necessary.
Object data = new A();
FieldInfo[] fields = data.GetType().GetFields();
String str = "";
foreach(FieldInfo f in fields){
str += f.Name + " = " + f.GetValue(data) + "\r\n";
}
Run Code Online (Sandbox Code Playgroud)
这是期望的结果:
a = A-val
b = B-val
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不起作用.请帮忙,谢谢.
我有一个泛型类型Store<T>,用于Activator创建这种类型的实例.现在,在使用Activator之后,我可以将结果对象的类型转换object回实例化类型吗?我知道我用来实例化泛型的类型.请参阅以下代码:
class Store<T> where T : IStorable
{}
class Beer : IStorable
{}
class BeerStore : Store<Beer>
{}
Type storeType = someObjectThatImplementsIStorable.GetType();
Type classType = typeof(Store<>);
Type[] typeParams = new Type[] { storeType };
Type constructedType = classType.MakeGenericType(typeParams);
object x = Activator.CreateInstance(constructedType, new object[] { someParameter });
Run Code Online (Sandbox Code Playgroud)
我想做的是这样的事情:
var store = (Store<typeof(objectThatImplementsIStorable)>)x;
Run Code Online (Sandbox Code Playgroud)
但由于显而易见的原因,这不起作用.作为替代方案,我试过:
var store = (Store<IStorable>)x;
Run Code Online (Sandbox Code Playgroud)
这可能在我看来有效,但给出了一个InvalidCastException.
如何再次访问Store<T>对象中我知道的方法x?
我正在尝试动态创建代理类.我知道有一些非常好的框架可以做到这一点,但这纯粹是一个宠物项目作为一个学习练习,所以我想自己做.
例如,如果我有以下类实现接口:
interface IMyInterface
{
void MyProcedure();
}
class MyClass : IMyInterface
{
void MyProcedure()
{
Console.WriteLine("Hello World");
}
}
Run Code Online (Sandbox Code Playgroud)
为了拦截这个类的方法以便记录它们,我正在创建另一个类(我的代理类版本),它实现相同的接口但包含对"真实"类的引用.此类执行操作(例如,日志记录),然后在真实类上调用相同的方法.
例如:
class ProxyClass : IMyInterface
{
private IMyInterface RealClass { get; set; }
void MyProcedure()
{
// Log the call
Console.WriteLine("Logging..");
// Call the 'real' method
RealClass.MyProcedure();
}
}
Run Code Online (Sandbox Code Playgroud)
然后调用者调用代理类上的所有方法(我使用基本的home-brew IoC容器来注入代理类来代替真正的类).我正在使用此方法,因为我希望能够RealClass在运行时将其换出到另一个实现相同接口的类.
有没有办法ProxyClass在运行时创建并填充其RealClass属性,以便它可以用作真实类的代理?是否有一种简单的方法可以做到这一点,还是需要使用类似的东西Reflection.Emit并生成MSIL?
在.Net中,给定一个类型名称,是否有一个方法告诉我在哪个程序集(System.Reflection.Assembly的实例)中定义了该类型?
我假设我的项目已经有了对该程序集的引用,只需要知道它是哪一个.
我希望自动发现有关提供的类的一些信息,以执行类似于表单输入的操作.具体来说,我使用反射为每个属性返回PropertyInfo值.我可以从我的"表单"中读取或写入每个属性的值,但如果属性被定义为"int",我将无法编写空值,我的程序甚至不能尝试.
如何使用反射来确定是否可以为给定属性分配空值,而无需编写switch语句来检查每种可能的类型?特别是我想检测像"int"和"int?"这样的盒装类型之间的区别,因为在第二种情况下我确实希望能够写出一个空值.IsValueType和IsByRef似乎没有区别.
public class MyClass
{
// Should tell me I cannot assign a null
public int Age {get; set;}
public DateTime BirthDate {get; set;}
public MyStateEnum State {get; set;}
public MyCCStruct CreditCard {get; set;}
// Should tell me I can assign a null
public DateTime? DateOfDeath {get; set;}
public MyFamilyClass Famly {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
请注意,我需要在实际尝试写入值之前很久才确定此信息,因此使用围绕SetValue的异常处理不是一种选择.
在C#中有没有办法获得调用方法的程序集?(不是当前的方法.)
即我想要执行程序集,一个在调用堆栈中.
我知道在Java中,我们可以通过创建一个类的实例new,clone(),Reflection和serializing and de-serializing.
我创建了一个实现Singleton的简单类.
我需要一直停止创建我的类的实例.
public class Singleton implements Serializable{
private static final long serialVersionUID = 3119105548371608200L;
private static final Singleton singleton = new Singleton();
private Singleton() { }
public static Singleton getInstance(){
return singleton;
}
@Override
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException("Cloning of this class is not allowed");
}
protected Object readResolve() {
return singleton;
}
//-----> This is my implementation to stop it but Its not working. …Run Code Online (Sandbox Code Playgroud) 以下代码是否有效?
void myMethod (Class classType) {
if (classType == MyClass.class) {
// do something
}
}
myMethod (OtherClass.class);
Run Code Online (Sandbox Code Playgroud)
如果没有,那么我可以检查传递的.class(Class Type)类型是否为MyClass?
感谢名单!
我正在使用一个不是开源的项目,我需要修改一个或多个类.
在一个类中是以下集合:
private Map<Integer, TTP> ttp = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
我需要做的就是使用反射并在这里使用concurrenthashmap.我试过跟随代码,但它不起作用.
Field f = ..getClass().getDeclaredField("ttp");
f.setAccessible(true);
f.set(null, new ConcurrentHashMap<>());
Run Code Online (Sandbox Code Playgroud)