我有一个API,我正在变成一个内部DSL.因此,我的PoJos中的大多数方法返回对此的引用,以便我可以声明性地将方法链接在一起(语法糖).
myComponent
.setID("MyId")
.setProperty("One")
.setProperty2("Two")
.setAssociation(anotherComponent)
.execute();
Run Code Online (Sandbox Code Playgroud)
我的API不依赖于Spring,但我希望通过PoJo友好,零参数构造函数,getter和setter使其成为"Spring-Friendly".问题是,当我有一个非void返回类型时,Spring似乎没有检测到我的setter方法.
在将命令链接在一起时,返回类型非常方便,所以我不想破坏我的编程API,只是为了兼容Spring注入.
Spring中有一个设置允许我使用非void setter吗?
克里斯
执行以下代码时,我收到此错误"无法对ContainsGenericParameters为true的类型或方法执行后期绑定操作."
class Program
{
static void Main(string[] args)
{
MethodInfo MI = typeof(MyClass).GetMethod("TestProc");
MI.MakeGenericMethod(new [] {typeof(string)});
MI.Invoke(null, new [] {"Hello"});
}
}
class MyClass
{
public static void TestProc<T>(T prefix)
{
Console.WriteLine("Hello");
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码只是我面临的实际问题的缩放版本.请帮忙.
foreach(var filter in filters)
{
var filterType = typeof(Filters);
var method = filterType.GetMethod(filter);
if (method != null) value = (string)method.Invoke(null, new[] { value });
}
Run Code Online (Sandbox Code Playgroud)
是否有一种不区分大小写的方法来获取方法?
我试图通过反射向IList添加项目,但在调用"添加"方法时,抛出错误"对象引用未设置".在调试时我发现GetMethod("Add")返回了一个NULL引用.
Type objTyp = typeof(MyObject); //HardCoded TypeName for demo purpose
var IListRef = typeof (List<>);
Type[] IListParam = {objTyp};
object Result = IListRef.MakeGenericType(IListParam);
MyObject objTemp = new MyObject();
Result.GetType().GetMethod("Add").Invoke(Result, new[] {objTemp });
Run Code Online (Sandbox Code Playgroud)
请帮忙.
我有一个win表单应用程序,其中列表框显示方法(按属性).我试图动态调用线程中的方法,使用反射从列表框的选定值获取方法信息.但是,当调用Methodinfo.Invoke时,我得到了这个内部异常"非静态方法需要一个目标C#".
这是我的代码(请记住,我仍然是c#和编程的新手.)
private void PopulateComboBox()
{//Populates list box by getting methods with declared attributes
MethodInfo[] methods = typeof(MainForm).GetMethods();
MyToken token = null;
List<KeyValuePair<String, MethodInfo>> items =
new List<KeyValuePair<string, MethodInfo>>();
foreach (MethodInfo method in methods)
{
token = Attribute.GetCustomAttribute(method,
typeof(MyToken), false) as MyToken;
if (token == null)
continue;
items.Add(new KeyValuePair<String, MethodInfo>(
token.DisplayName, method));
}
testListBox.DataSource = items;
testListBox.DisplayMember = "Key";
testListBox.ValueMember = "Value";
}
public void GetTest()
{//The next two methods handle selected value of the listbox and invoke …Run Code Online (Sandbox Code Playgroud) 我有一些问题,通过反射调用带有out参数的重载静态方法,并会欣赏一些指针.
我期待动态地创建类型像System.Int32或者System.Decimal,然后调用静态TryParse(string, out x)方法就可以了.
以下代码有两个问题:
t.GetMethod("TryParse", new Type[] { typeof(string), t } ) 无法返回我期望的MethodInfo
mi.Invoke(null, new object[] { value.ToString(), concreteInstance })似乎成功但没有将out参数concreteInstance设置为已解析的值
交织到此函数中,您可以看到一些临时代码,演示如果type参数设置为应该发生什么System.Decimal.
public static object Cast(object value, string type)
{
Type t = Type.GetType(type);
if (t != null)
{
object concreteInstance = Activator.CreateInstance(t);
decimal tempInstance = 0;
List<MethodInfo> l = new List<MethodInfo>(t.GetMethods(BindingFlags.Static | BindingFlags.Public));
MethodInfo mi;
mi = t.GetMethod("TryParse", new Type[] { typeof(string), t } ); //this …Run Code Online (Sandbox Code Playgroud) 我正试图找到"打破非公开方法"的解决方案.
我只是想调用RuntimeMethodInfo.InternalGetCurrentMethod(...),传递我自己的参数(所以我可以实现GetCallingMethod()),或直接RuntimeMethodInfo.InternatGetCurrentMethod(ref StackCrawlMark.LookForMyCaller)在我的日志记录例程中使用.GetCurrentMethod实施方式如下:
[MethodImpl(MethodImplOptions.NoInlining)]
public static MethodBase GetCurrentMethod()
{
StackCrawlMark lookForMyCaller = StackCrawlMark.LookForMyCaller;
return RuntimeMethodInfo.InternalGetCurrentMethod(ref lookForMyCaller);
}
Run Code Online (Sandbox Code Playgroud)
InternalGetCurrentMethod声明在哪里:内部:-).
我使用反射调用方法没有问题,但这会弄乱调用堆栈,这只是必须要保留的一件事,否则它会破坏它的目的.
我的堆栈跟踪保持接近原始的几率是多少(至少在允许的StackCrawlMarks 的距离内,这是LookForMe,LookForMyCaller和LookForMyCallersCaller.是否有一些复杂的方法来实现我想要的?
我有一个通用类型如下
public class TestGeneric<T>
{
public T Data { get; set; }
public TestGeneric(T data)
{
this.Data = data;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我现在有一个对象(来自某个外部源)我知道它的类型是一些封闭的TestGeneric <>,但我不知道TypeParameter T.现在我需要访问我的对象的数据.问题是我无法强制转换对象,因为我不确切知道哪个封闭的TestGeneric.
我用
// thx to http://stackoverflow.com/questions/457676/c-reflection-check-if-a-class-is-derived-from-a-generic-class
private static bool IsSubclassOfRawGeneric(Type rawGeneric, Type subclass)
{
while (subclass != typeof(object))
{
var cur = subclass.IsGenericType ? subclass.GetGenericTypeDefinition() : subclass;
if (rawGeneric == cur)
{
return true;
}
subclass = subclass.BaseType;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
为了确保,我的对象是泛型类型.有问题的代码如下:
public static void Main()
{
object myObject = new TestGeneric<string>("test"); // or from another …Run Code Online (Sandbox Code Playgroud) 我正在为个人项目进行反思繁重的工作,我需要通过其Type对象访问委托的返回类型.这是一个小元,所以这是一个例子.
Type type = typeof(Func<Foo, Bar, Baz>);
// ????
// Use reflection to come to the following expected result
Type result = typeof(Baz);
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我将不会有任何类型的实例进入Delegate.
我有一个类似于下面的界面:
public interface IInterface<T>
where T : IInterface<T>
{
}
Run Code Online (Sandbox Code Playgroud)
现在我需要使用反射创建一个表示此接口的类型,例如
typeof(IInterface<>).MakeGenericType(someType);
Run Code Online (Sandbox Code Playgroud)
但是,我实际上并不知道什么类型的'someType'将在运行时之前,并且该类型可能无法作为通用接口的类型参数有效,因此MakeGenericType失败.
问题是,如何检查'someType'是否对泛型约束有效?