由于各种原因,我需要在C#中实现类型缓存机制.幸运的是,CLR提供Type.GUID了唯一标识类型.不幸的是,我找不到任何方法来查找基于此GUID的类型.有Type.GetTypeFromCLSID(),但根据我的文档(和实验)的理解是,做了非常,非常不同.
是否有任何方法可以获得基于其GUID的类型,而不是循环遍历所有已加载的类型并与其GUID进行比较?
编辑:我忘了提到我真的很喜欢固定宽度的"类型指纹",这就是为什么GUID对我这么有吸引力.当然,在一般情况下,该类型的完全限定名称可以使用.
我有这两个班:
Item<T> : BusinessBase<T> where T : Item<T>
{
public static T NewItem()
{
//some code here
}
}
Video : Item <Video>
{
}
Run Code Online (Sandbox Code Playgroud)
现在我想使用反射在类Video上调用NewItem()方法.当我尝试这个:
MethodInfo inf = typeof(Video).GetMethod("NewItem", BindingFlags.Static);
Run Code Online (Sandbox Code Playgroud)
执行此行后的对象inf仍为null.我可以在类Video上调用静态NewItem()方法吗?
在我的c#应用程序中,我通常使用以下代码获取版本(以显示客户):
System.Reflection.Assembly.GetExecutingAssembly().GetName().Version
Run Code Online (Sandbox Code Playgroud)
这在Windows Phone 7中不起作用(它会挂起模拟器,而电话崩溃对MS来说是禁忌).
那么,我如何在Windows Phone 7设备上获得执行的版本?
[更新]如下面的评论中所述,在wp7应用程序中调用GetName()似乎是个问题.
如何获取当前正在执行的程序集引用的所有程序集的路径?GetReferencedAssmblies()给了我AssemblyName[].我如何从那里到达它们的装载位置?
我正在尝试使用PHP反射根据控制器方法中的参数类型自动动态加载模型的类文件.这是一个示例控制器方法.
<?php
class ExampleController
{
public function PostMaterial(SteelSlugModel $model)
{
//etc...
}
}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所拥有的.
//Target the first parameter, as an example
$param = new ReflectionParameter(array('ExampleController', 'PostMaterial'), 0);
//Echo the type of the parameter
echo $param->getClass()->name;
Run Code Online (Sandbox Code Playgroud)
这是有效的,输出将是'SteelSlugModel',如预期的那样.但是,有可能模型的类文件可能尚未加载,并且使用getClass()要求定义类 - 为什么我这样做的一部分是自动加载控制器操作可能需要的任何模型.
有没有办法获取参数类型的名称,而无需先加载类文件?
方法也是如此:
我得到了两个PropertyInfo实例或者从它们所在的类GetProperty()或者GetMember()等等中提取的方法(或者可能来自MemberExpression).
我想确定它们是否实际上指的是相同的属性或相同的方法
(propertyOne == propertyTwo)
Run Code Online (Sandbox Code Playgroud)
要么
(methodOne == methodTwo)
Run Code Online (Sandbox Code Playgroud)
显然,这不会真正起作用,您可能正在查看相同的属性,但它可能是从类层次结构的不同级别(通常在这种情况下propertyOne != propertyTwo)中提取的
当然,我可以查看DeclaringType,并重新请求该属性,但是当你开始考虑时,这开始变得有点混乱
在一天结束时,我只是想能够在两个属性或两个方法之间进行智能相等检查,我80%确定上述要点并未涵盖所有边缘情况,而我我可以坐下来,写一堆测试并开始玩,我很清楚我对这些概念实际实现方式的低级知识并不是很好,我希望这是一个已经回答的主题,我只是吮吸搜索.
最好的答案会给我一些实现上述目标的方法,解释已经处理了哪些边缘情况以及为什么:-)
澄清:
从字面上看,我想确保它们是同一个属性,这里有一些例子
public interface IFoo
{
string Bar { get; set; }
}
public class Foo : IFoo
{
string Bar { get; set; }
}
typeof(IFoo).GetProperty("Bar")
Run Code Online (Sandbox Code Playgroud)
和
typeof(Foo).GetProperty("Bar")
Run Code Online (Sandbox Code Playgroud)
将返回两个不相等的属性信息:
public class BaseClass
{
public string SomeProperty { get; set ; }
}
public class DerivedClass : BaseClass { }
typeof(BaseClass).GetMethod("SomeProperty")
Run Code Online (Sandbox Code Playgroud)
和 …
我在单元测试中有以下代码
public bool TestMethodsOf<T, I>()
{
var impl = typeof(T);
var valid = true;
foreach (var iface in impl.GetInterfaces().Where(i => typeof(I).IsAssignableFrom(i)))
{
var members = iface.GetMethods();
foreach (var member in members)
{
Trace.Write("Checking if method " + iface.Name + "." + member.Name + " is virtual...");
var implMember = impl.GetMethod(member.Name, member.GetParameters().Select(c => c.ParameterType).ToArray());
if (!implMember.IsVirtual)
{
Trace.WriteLine(string.Format("FAILED"));
valid = false;
continue;
}
Trace.WriteLine(string.Format("OK"));
}
}
return valid;
}
Run Code Online (Sandbox Code Playgroud)
我打电话给他
Assert.IsTrue(TestMethodsOf<MyView, IMyView>());
Run Code Online (Sandbox Code Playgroud)
我想确保界面中的所有方法都声明为虚拟.原因是因为我正在应用spring.net方面,它只适用于虚方法.
我遇到的问题是implMember.IsVirtual总是正确的,即使它们在声明类型中没有声明它们也是如此.
我的TestMethodsOf逻辑有什么问题?
干杯
使用反射我需要检索a的属性值 Nullable Type of DateTime
我怎样才能做到这一点?
当我尝试propertyInfo.GetValue(object, null)它不起作用.
谢谢
我的代码:
var propertyInfos = myClass.GetType().GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfos)
{
object propertyValue= propertyInfo.GetValue(myClass, null);
}
Run Code Online (Sandbox Code Playgroud)
propertyValue结果对于可空类型始终为null
我要做的是使用Reflection调用属性的方法.我有原始的Control(一个ComboBox),属性的PropertyInfo(ComboBox.Items)和方法的名称(ComboBox.Items.Add).我已经尝试了下面的代码来获取,更改,设置但它不起作用,因为Items没有setter.
PropertyInfo p = controlType.GetProperty(propertyName); // gets the property ('Items')
MethodInfo m = p.PropertyType.GetMethod(methodName); // gets the method ('Items.Add')
object o = p.GetValue(newControl, null); // gets the current 'Items'
m.Invoke(o, new object[] { newValue }); // invokes 'Add' which works
p.SetValue(newControl, o, null); // exception: 'Items' has no setter
Run Code Online (Sandbox Code Playgroud)
有人有建议吗?
谢谢
我有以下代码:
public void myMethod(Object... args) {
System.out.println("this is myMethod");
}
public void invokeMyMethod() {
Method s = this.getClass().getMethod("myMethod", Object[].class);
Object[] ex = new Object[2];
ex[0] = "hi";
ex[1] = "there";
s.invoke(this, ex);
}
Run Code Online (Sandbox Code Playgroud)
我得到异常java.lang.IllegalArgumentException:错误的参数数量.怎么了?