我有这种类型,包含泛型方法的两个重载.我喜欢Func<T>使用反射检索其中一个重载(使用参数).但问题是我找不到正确的参数类型来提供Type.GetMethod(string, Type[])方法.
这是我的班级定义:
public class Foo
{
public void Bar<T>(Func<T> f) { }
public void Bar<T>(Action<T> a) { }
}
Run Code Online (Sandbox Code Playgroud)
这是我想出来的,不幸的是没有成功:
[TestMethod]
public void Test1()
{
Type parameterType = typeof(Func<>);
var method = typeof(Foo).GetMethod("Bar", new Type[] { parameterType });
Assert.IsNotNull(method); // Fails
}
Run Code Online (Sandbox Code Playgroud)
如何获得MethodInfo我知道参数的通用方法?
public enum A {
A(1);
private A(int i){
}
private A(){
super(); // compile - error
// Cannot invoke super constructor from enum constructor A()
}
}
Run Code Online (Sandbox Code Playgroud)
这是enum A从abstract java.lang.Enumextends扩展的层次结构java.lang.Object
Class c = Class.forName("/*path*/.A");
System.out.println(c.getSuperclass().getName());
System.out.println(Modifier.toString(c.getSuperclass().getModifiers()).contains("abstract"));
System.out.println(c.getSuperclass().getSuperclass().getName());
Run Code Online (Sandbox Code Playgroud) 我想使用自动加载来使用现有数据库.我知道如何在没有声明性语法(model/_ init _.py)的情况下执行此操作:
def init_model(engine):
"""Call me before using any of the tables or classes in the model"""
t_events = Table('events', Base.metadata, schema='events', autoload=True, autoload_with=engine)
orm.mapper(Event, t_events)
Session.configure(bind=engine)
class Event(object):
pass
Run Code Online (Sandbox Code Playgroud)
这工作正常,但我想使用声明性语法:
class Event(Base):
__tablename__ = 'events'
__table_args__ = {'schema': 'events', 'autoload': True}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这样我得到:
sqlalchemy.exc.UnboundExecutionError:没有引擎绑定到此Table的MetaData.通过autoload_with = <someengine>将引擎传递给表,或通过metadata.bind = <someengine>将MetaData与引擎相关联
这里的问题是我不知道在导入模型的阶段从哪里获取引擎(在autoload_with中使用它)(它在init_model()中可用).我尝试添加
meta.Base.metadata.bind(engine)
Run Code Online (Sandbox Code Playgroud)
到environment.py但它不起作用.有人找到了一些优雅的解决方案
我有一个对象列表,其中我无法知道编译时的类型.
我需要识别存在'Count'属性的任何这些对象,如果存在则获取值.
此代码适用于简单的Collection类型:
PropertyInfo countProperty = objectValue.GetType().GetProperty("Count");
if (countProperty != null)
{
int count = (int)countProperty.GetValue(objectValue, null);
}
Run Code Online (Sandbox Code Playgroud)
问题是这不适用于泛型类型,例如IDictionary<TKey,TValue>.在这些情况下,即使实例化对象中存在"Count"属性,'countProperty'值也会返回null.
我想要做的就是识别任何基于集合/字典的对象并找到它的大小(如果有的话).
编辑:根据要求,这是完整的代码列表,不起作用
private static void GetCacheCollectionValues(ref CacheItemInfo item, object cacheItemValue)
{
try
{
//look for a count property using reflection
PropertyInfo countProperty = cacheItemValue.GetType().GetProperty("Count");
if (countProperty != null)
{
int count = (int)countProperty.GetValue(cacheItemValue, null);
item.Count = count;
}
else
{
//poke around for a 'values' property
PropertyInfo valuesProperty = cacheItemValue.GetType().GetProperty("Values");
int valuesCount = -1;
if (valuesProperty != null)
{ …Run Code Online (Sandbox Code Playgroud) 我编写了一个日志类和一个函数,如下面的代码所示:
Log(System.Reflection.MethodBase methodBase, string message)
Run Code Online (Sandbox Code Playgroud)
每次我记录某些东西时,我也会从methodBase.Name和methodBase.DeclaringType.Name中记录类名.
我阅读了以下文章使用Get CurrentMethod,我注意到这个方法很慢.
我应该使用this.GetType()而不是System.Reflection.MethodBase,或者我应该在我的日志中手动记录类/方法名称,例如Log("ClassName.MethodName","log message")?最佳做法是什么?
我有一个名为chooseDialog(String s,int i)的静态方法,其中我想根据提供给chooseDialog的参数在同一个类(Dialogs.class)中调用另一个方法.s是所需方法的名称,我是它的单个参数.
我已经尝试了很多教程,花了几个小时阅读这个主题,但我似乎无法掌握我究竟需要做什么.
有任何想法吗?
谢谢!
我得到了value1,value2当它们应该是相同时,它们都是零并且不相等.
我怎么能比较这两个对象的值?
private bool CheckProjectIsUnique(
TBR.Domain.Project project,
List<UniqueProjectType> types,
out UniqueProjectType uniqueCandidate)
{
uniqueCandidate = CreateUniqueProjectType(project);
if (types.Count == 0)
return true;
foreach (UniqueProjectType type in types)
{
bool exists = true;
foreach (PropertyInfo prop in type.GetType().GetProperties())
{
var value1 = prop.GetValue(type, null);
var value2 = prop.GetValue(uniqueCandidate, null);
if (value1 != value2)
{
exists = false;
break;
}
}
if (exists)
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试反序列化,Generic<T> where T : struct但是当我调用时,ctor.Invoke(args); 我得到异常"无法创建实例,因为Type.ContainsGenericParameters为true".
如何传递我想要的泛型类型?
嗨,我需要在运行时使用反射为列表对象创建实例.例如,我有2个类,如下所示,
class Class1
{
List<Class2> class2List;
public List<Class2> Class2List
{
get;set;
}
}
class Class2
{
public string mem1;
public string mem2;
}
Run Code Online (Sandbox Code Playgroud)
Class1在另一个类中创建Runtime 实例后Class3,我想为该类的所有属性赋值.在这种情况下,Class2List是属性List<Class2>.在运行时,我不知道类的类型List<Class2>.如何List<Class2>在运行时初始化属性,即在class3内部.
任何建议都非常感谢...
这是另一个问题"我如何用Java做这个?" 在Python中,我可以使用'*'符号来解压缩参数,如下所示:
>>> range(3, 6) # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> range(*args) # call with arguments unpacked from a list
[3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
Java支持使用...args语法获取args列表,但是有没有办法(可能使用Reflection库?)来解包那些其他函数?
reflection ×10
c# ×5
.net ×3
java ×3
generics ×2
android ×1
declarative ×1
enumeration ×1
instance ×1
methodbase ×1
methods ×1
performance ×1
pylons ×1
python ×1
sqlalchemy ×1
superclass ×1