这可能是不可能的,但我有这个课程:
public class Metadata<DataType> where DataType : struct
{
private DataType mDataType;
}
Run Code Online (Sandbox Code Playgroud)
还有更多,但让我们保持简单.泛型类型(DataType)仅限于where语句的值类型.我想要做的是拥有不同类型(DataType)的这些元数据对象的列表.如:
List<Metadata> metadataObjects;
metadataObjects.Add(new Metadata<int>());
metadataObjects.Add(new Metadata<bool>());
metadataObjects.Add(new Metadata<double>());
Run Code Online (Sandbox Code Playgroud)
这甚至可能吗?
编辑:
当然,我的真实代码看起来并不完全像这样.我试着编写半伪代码,以便更清楚地表达我想做的事情.
看起来它只是把事情搞砸了.
所以,我真正想做的是:
Method<Interface1>();
Method<Interface2>();
Method<Interface3>();
...
Run Code Online (Sandbox Code Playgroud)
嗯......我想也许我可以用反射把它变成一个循环.所以问题是:我该怎么做.我有很反射的浅识.所以代码示例会很棒.
场景如下所示:
public void Method<T>() where T : class
{}
public void AnotherMethod()
{
Assembly assembly = Assembly.GetExecutingAssembly();
var interfaces = from i in assembly.GetTypes()
where i.Namespace == "MyNamespace.Interface" // only interfaces stored here
select i;
foreach(var i in interfaces)
{
Method<i>(); // Get compile error here!
}
Run Code Online (Sandbox Code Playgroud)
原帖:
嗨!
我正在尝试遍历命名空间中的所有接口,并将它们作为参数发送到这样的泛型方法:
public void Method<T>() where T : class
{}
public void AnotherMethod()
{
Assembly assembly = Assembly.GetExecutingAssembly();
var interfaces = …
Run Code Online (Sandbox Code Playgroud) 我有一个通用的方法
bool DoesEntityExist<T>(Guid guid, ITransaction transaction) where T : IGloballyIdentifiable;
Run Code Online (Sandbox Code Playgroud)
如何以下列方式使用该方法:
Type t = entity.GetType();
DoesEntityExist<t>(entityGuid, transaction);
Run Code Online (Sandbox Code Playgroud)
我一直收到愚蠢的编译错误:
找不到类型或命名空间名称't'(您是否缺少using指令或程序集引用?)
DoesEntityExist<MyType>(entityGuid, transaction);
Run Code Online (Sandbox Code Playgroud)
完美的工作,但我不想使用if指令每次调用具有单独类型名称的方法.
我想用给定的类型对象调用我的泛型方法.
void Foo(Type t)
{
MyGenericMethod<t>();
}
Run Code Online (Sandbox Code Playgroud)
显然不起作用.
我怎样才能使它工作?
我有一个通用的方法
Foo<T>
Run Code Online (Sandbox Code Playgroud)
我有一个Type变量 bar
是否有可能实现类似的目标 Foo<bar>
Visual Studio期望在栏上有一个类型或命名空间.
善良,
担
我有个问题.是否可以在.NET中使用反射调用泛型方法?我尝试了以下代码
var service = new ServiceClass();
Type serviceType = service.GetType();
MethodInfo method = serviceType.GetMethod("Method1", new Type[]{});
method.MakeGenericMethod(typeof(SomeClass));
var result = method.Invoke(service, null);
Run Code Online (Sandbox Code Playgroud)
但它抛出以下异常"无法对ContainsGenericParameters为true的类型或方法执行后期绑定操作."
是否可以在设计时不知道类型的情况下声明泛型的实例?
例:
Int i = 1;
List<typeof(i)> list = new List<typeof(i)>();
Run Code Online (Sandbox Code Playgroud)
我的类型可以是任何东西,而不是必须做:
List<int> list = new List<int();
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
public class ClassExample
{
void DoSomthing<T>(string name, T value)
{
SendToDatabase(name, value);
}
public class ParameterType
{
public readonly string Name;
public readonly Type DisplayType;
public readonly string Value;
public ParameterType(string name, Type type, string value)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException("name");
if (type == null)
throw new ArgumentNullException("type");
this.Name = name;
this.DisplayType = type;
this.Value = value;
}
}
public void GetTypes()
{
List<ParameterType> l = report.GetParameterTypes();
foreach (ParameterType p in l)
{
DoSomthing<p.DisplayType>(p.Name, (p.DisplayType)p.Value);
}
}
} …
Run Code Online (Sandbox Code Playgroud) 可能重复:
如何使用反射来调用泛型方法?
选择具有反射的右通用方法
嗨,您好
假设我在类中有以下两种方法:
public void MyMethod(object val) {}
public void MyMethod<T>(T val) {}
Run Code Online (Sandbox Code Playgroud)
通过反射,我可以得到第一个这样的方法:
Type[] typeArray = new Type[1];
typeArray.SetValue(typeof(object), 1);
var myMethod = myInstance.GetType().GetMethod("MyMethod", typeArray);
Run Code Online (Sandbox Code Playgroud)
但是我如何获得第二种通用方法呢?
我想得到方法System.Linq.Queryable.OrderyBy<T, TKey>(the IQueryable<T> source, Expression<Func<T,TKey>> keySelector)
方法,但我不断提出空值.
var type = typeof(T);
var propertyInfo = type.GetProperty(group.PropertyName);
var propertyType = propertyInfo.PropertyType;
var sorterType = typeof(Func<,>).MakeGenericType(type, propertyType);
var expressionType = typeof(Expression<>).MakeGenericType(sorterType);
var queryType = typeof(IQueryable<T>);
var orderBy = typeof(System.Linq.Queryable).GetMethod("OrderBy", new[] { queryType, expressionType }); /// is always null.
Run Code Online (Sandbox Code Playgroud)
有没有人有任何见解?我宁愿不循环GetMethods
结果.