我在使用反射转储一些对象属性的例程上得到此错误,类似于下面的代码.
MemberInfo[] members = obj.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance) ;
foreach (MemberInfo m in members)
{
PropertyInfo p = m as PropertyInfo;
if (p != null)
{
object po = p.GetValue(obj, null);
...
}
}
Run Code Online (Sandbox Code Playgroud)
实际错误是"调用目标抛出了异常",内部异常为"只能在Type.IsGenericParameter为true的Type上调用Method".
在调试器obj的这个阶段出现
{Name = "SqlConnection" FullName = "System.Data.SqlClient.SqlConnection"}
Run Code Online (Sandbox Code Playgroud)
使用System.RuntimeType类型
方法m是{System.Reflection.MethodBase DeclaringMethod}
请注意,obj的类型为System.RuntimeType,成员包含188个项目,而简单的typeof(System.Data.SqlClient.SqlConnection).GetMembers(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)仅返回65.
我尝试在obj和p.PropertyType上检查isGenericParameter,但对于大多数属性(包括p.GetValue工作的那些属性),这似乎都是错误的.
那么究竟什么是"Type.IsGenericParameter为真的类型",更重要的是如何在没有try/catch的情况下避免这个错误?
如何获取parms的值(使用反射循环).在之前的问题中,有人向我展示了如何使用反射来遍历parms.
static void Main(string[] args)
{
ManyParms("a","b","c",10,20,true,"end");
Console.ReadLine();
}
static void ManyParms(string a, string b, string c, int d, short e, bool f, string g)
{
var parameters = MethodBase.GetCurrentMethod().GetParameters();
foreach (ParameterInfo parameter in parameters)
{
string parmName = parameter.Name;
Console.WriteLine(parmName);
//Following idea required an object first
//Type t = this.GetType();
//t.GetField(parmName).GetValue(theObject));
}
}
Run Code Online (Sandbox Code Playgroud)
如果您必须知道我为什么要这样做,请参见此处: .NET反映所有方法参数
谢谢大家 - 似乎在Python,PERL,PHP中这很容易.
即使它可能不是反射,如果我使用反射来获取字段名称,似乎有一种简单的动态方式来获取基于名称的值.我还没有尝试过AOP(Aspect Oriented Programming)解决方案.如果我不能在一两个小时内做到这一点,我可能不会这样做.
我正在使用反射来获取TryParse方法信息(为第一个人提供upvote以猜测原因;).
如果我打电话:
typeof(Int32).GetMethod("Parse",
BindingFlags.Static | BindingFlags.Public,
null,
new Type[] { typeof(string) },
null);
Run Code Online (Sandbox Code Playgroud)
我得到一个方法,但稍微扩展一下:
typeof(Int32).GetMethod("TryParse",
BindingFlags.Static | BindingFlags.Public,
null,
new Type[] { typeof(string), typeof(Int32) },
null);
Run Code Online (Sandbox Code Playgroud)
我一无所获.我的spidersense告诉我这是因为第二个参数是out参数.
谁知道我在这里做错了什么?
这可能是一个愚蠢的问题,但我试图ViewBag像这样填充一个匿名对象:
ViewBag.Stuff = new { Name = "Test", Email = "user@domain.com" };
Run Code Online (Sandbox Code Playgroud)
并从这样的视图访问它:
@ ViewBag.Stuff.Name
我理解ViewBag是动态的,"Stuff"是一个匿名对象...但是当我从上面的View行看调试器时,我可以看到具有正确值的所有属性.为什么模型活页夹有这么难?
有没有一个很好的方法来实现这个而不创建一个模型类?我想继续使用new {}
除了解析函数文件之外,有没有办法在matlab中获取函数的输入和输出参数的名称?
例如,给定以下函数文件:
divide.m
function [value, remain] = divide(left, right)
value = floor(left / right);
remain = left / right - value;
end
Run Code Online (Sandbox Code Playgroud)
从函数外部,我想得到一个输出参数数组,在这里:['value', 'remain']和输入参数类似:['left', 'right'].
在matlab中有一个简单的方法吗?Matlab通常似乎很好地支持反射.
编辑背景:
这样做的目的是在窗口中显示功能参数以供用户输入.我正在编写一种信号处理程序,对这些信号执行操作的函数存储在子文件夹中.我已经有一个列表和用户可以选择的每个函数的名称,但是某些函数需要额外的参数(例如,平滑函数可能会将窗口大小作为参数).
目前,我可以在程序将找到的子文件夹中添加一个新功能,用户可以选择它来执行操作.我缺少的是用户指定输入和输出参数,这里我遇到了障碍,因为我找不到函数的名称.
我想获取类的所有方法,包括public,protected,package和private方法,以及包括继承的方法.
记得:
Class.getDeclaredMethods()获取public,protected,package和private方法,但不包括继承的方法.Class.getMethods获取继承的方法,但只获取公共方法.在Java 8之前,我们可以做一些事情:
Collection<Method> found = new ArrayList<Method>();
while (clazz != null) {
for (Method m1 : clazz.getDeclaredMethods()) {
boolean overridden = false;
for (Method m2 : found) {
if (m2.getName().equals(m1.getName())
&& Arrays.deepEquals(m1.getParameterTypes(), m2
.getParameterTypes())) {
overridden = true;
break;
}
}
if (!overridden) found.add(m1);
}
clazz = clazz.getSuperclass();
}
return found;
Run Code Online (Sandbox Code Playgroud)
但是现在,如果类使用默认方法实现某些接口而不被具体超类覆盖,则这些方法将逃避上述检测.此外,现在有关于具有相同名称的默认方法的规则,并且还必须考虑这些规则.
问题:获取类的所有方法的当前推荐方法是什么:
"all"的最常见定义应该是可以在类的实例方法中直接访问的方法,而不使用super或类名:
我有一个Kotlin类,其主要(也是唯一的)构造函数为空.
我引用了这个类:
val kClass: KClass<MyClass> = MyClass::class
Run Code Online (Sandbox Code Playgroud)
如何使用反射创建此类的实例?
在Java中我会这样做,myClass.newInstance()但在Kotlin中我似乎需要先找到构造函数:
kClass.constructors.first().call()
Run Code Online (Sandbox Code Playgroud)
我已经看到primaryConstructor在一些错误报告中提到但它没有出现在我的IDE中.
这可能是最好的例子.我有一个属性的枚举:
public enum MyEnum {
[CustomInfo("This is a custom attrib")]
None = 0,
[CustomInfo("This is another attrib")]
ValueA,
[CustomInfo("This has an extra flag", AllowSomething = true)]
ValueB,
}
Run Code Online (Sandbox Code Playgroud)
我想从实例中获取这些属性:
public CustomInfoAttribute GetInfo( MyEnum enumInput ) {
Type typeOfEnum = enumInput.GetType(); //this will be typeof( MyEnum )
//here is the problem, GetField takes a string
// the .ToString() on enums is very slow
FieldInfo fi = typeOfEnum.GetField( enumInput.ToString() );
//get the attribute from the field
return fi.GetCustomAttributes( typeof( CustomInfoAttribute ), false …Run Code Online (Sandbox Code Playgroud) 我有一个专门的列表,其中包含以下类型的项目IThing:
public class ThingList : IList<IThing>
{...}
public interface IThing
{
Decimal Weight { get; set; }
Decimal Velocity { get; set; }
Decimal Distance { get; set; }
Decimal Age { get; set; }
Decimal AnotherValue { get; set; }
[...even more properties and methods...]
}
Run Code Online (Sandbox Code Playgroud)
有时我需要知道列表中所有内容的某个属性的最大值或最小值.因为"告诉不要问"我们让列表弄明白:
public class ThingList : IList<IThing>
{
public Decimal GetMaximumWeight()
{
Decimal result = 0;
foreach (IThing thing in this) {
result = Math.Max(result, thing.Weight);
}
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
这是非常好的.但有时我需要最小重量,有时最大速度等等.我不希望 …
可能重复:
.NET反射的代价是多少?
我遇到的问题的"优雅"解决方案是使用属性将类及其属性与另一个类相关联.问题是,要将它转换为另一个,我必须使用反射.我正在考虑将其托管在云端的服务器端应用程序.
我听过很多关于"反射很慢,不使用它"的隆隆声,慢得多慢?它是如此占用CPU密集度,以至于它会大大增加我的CPU时间,以至于我真的要为我决定在云端架构底部使用反射付出代价吗?
reflection ×10
c# ×7
.net ×3
.net-2.0 ×1
attributes ×1
enums ×1
inheritance ×1
java ×1
java-8 ×1
kotlin ×1
matlab ×1
oop ×1
vb.net ×1
viewbag ×1