我怎样才能得到我想要的类型?
我的项目中有很多
这样的类(非常旧且稳定的代码,我无法对它们进行很多更改,也许轻微的更改就可以)
public class MyEntity
{
public long ID { get; set; }
public string Name { get; set; }
public decimal Salary { get; set; }
public static GetMyEntity ( long ID )
{
MyEntity e = new MyEntity();
// load data from DB and bind to this instance
return e;
}
}
出于某些原因,现在我需要这样做:
Type t = Type.GetType("XXX"); // XXX is one of the above classes' name
MethodInfo staticM= t.GetMethods(BindingFlags.Public | BindingFlags.Static).FirstOrDefault();// I'm sure I can get the correct one
var o = staticM.Invoke(...); //returns a object, but I want the type above!
如果我一开始就传递“MyEntity”,我希望我能得到o作为MyEntity! 请注意我只知道“班级名称”。 MyEntity e = staticM.Invoke(...) as MyEntity;
不能在此处使用。
编辑
我正在尝试用表达式来解决它。
ParameterExpression[] parameterExps = (from p in staticM.GetParameters()
select Expression.Parameter(p.ParameterType, p.Name)).ToArray();
MethodCallExpression methodCallExp = Expression.Call(staticM, parameterExps);
BlockExpression blockExpression = Expression.Block(methodCallExp);
LambdaExpression lambdaExp = Expression.Lambda(blockExpression, parameterExps);
var d = lambdaExp.Compile() as Func<XX1,XX2>;
在示例 MyEntity 中,XX1 会很长,XX2 将是 MyEntity,但我如何编写它以适应其他情况?
由于没有解决方案,我将继续使用反射来操作返回对象......
There are a lot
of such classes in my project (very old and stable code, I can't do many changes to them, maybe slight changes are OK)
public class MyEntity
{
public long ID { get; set; }
public string Name { get; set; }
public decimal Salary { get; set; }
public static GetMyEntity ( long ID )
{
MyEntity e = new MyEntity();
// load data from DB and bind to this instance
return e;
}
}
For some reasons, now I need to do this:
Type t = Type.GetType("XXX"); // XXX is one of the above classes' name
MethodInfo staticM= t.GetMethods(BindingFlags.Public | BindingFlags.Static).FirstOrDefault();// I'm sure I can get the correct one
var o = staticM.Invoke(...); //returns a object, but I want the type above!
If I pass "MyEntity" at beginning, I hope I can get o as MyEntity! Please NOTE that I know the "name of the class" only. MyEntity e = staticM.Invoke(...) as MyEntity;
can't be used here.
EDIT
I'm trying to resolve it with expressions.
ParameterExpression[] parameterExps = (from p in staticM.GetParameters()
select Expression.Parameter(p.ParameterType, p.Name)).ToArray();
MethodCallExpression methodCallExp = Expression.Call(staticM, parameterExps);
BlockExpression blockExpression = Expression.Block(methodCallExp);
LambdaExpression lambdaExp = Expression.Lambda(blockExpression, parameterExps);
var d = lambdaExp.Compile() as Func<XX1,XX2>;
In the sample MyEntity, XX1 will be long, XX2 will be MyEntity, but how can I write it to fit other cases?
Since no solutions, I'll continue using reflection to operate the return object...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
物品退回后您将如何处理该物品?
如果调用代码知道它是什么,那么就可以在那里进行转换。
实际上,您在这里迫切需要接口,因为可以将返回值转换为已知接口,然后可以调用适当的方法。
WHat are you going to do with the obejct once it has been returned?
If the caling code knows what it is, then it can be casted there.
Really you are crying out for interfaces here, as the return can be cast ot a known interface, then the appropriate methods can be called.
如果您的字符串“XXX”是动态生成的(例如,根据用户输入),则无法进行编译时检查,因此无法让
o
除了object< 之外的任何内容。 /代码>。
但是,如果它是硬编码字符串,或者您知道编译类型的类型,则可以进行强制转换:
There's no way to get compile-time checking if your string "XXX" is generated dynamically (from user input, for example), and therefore there's no way to have
o
be anything butobject
.If it's a hard-coded string, however, or if you otherwise know the type at compile-type, you can do a cast: