当在编译时未知类型参数但是在运行时动态获取时,调用泛型方法的最佳方法是什么?
考虑以下示例代码 - 在Example()方法内部,GenericMethod<T>()使用Type存储在myType变量中调用的最简洁方法是什么?
public class Sample
{
public void Example(string typeName)
{
Type myType = FindType(typeName);
// What goes here to call GenericMethod<T>()?
GenericMethod<myType>(); // This doesn't work
// What changes to call StaticMethod<T>()?
Sample.StaticMethod<myType>(); // This also doesn't work
}
public void GenericMethod<T>()
{
// ...
}
public static void StaticMethod<T>()
{
//...
}
}
Run Code Online (Sandbox Code Playgroud) 是否可以实现以下代码?我知道它不起作用,但我想知道是否有解决方法?
Type k = typeof(double);
List<k> lst = new List<k>();
Run Code Online (Sandbox Code Playgroud) 我首先使用Entity Framework 4.3.1代码进行显式迁移.如何在实体配置类或迁移中添加列的描述,以便最终作为SQL Server中列的描述(例如2008 R2)?
我知道我可以编写一个DbMigration类的扩展方法,该方法将迁移事务中的sql迁移操作注册sp_updateextendedproperty或sp_addextendedproperty过程调用,并在迁移Up方法中创建表后调用该扩展.但是,我还没有发现一种优雅的内置方式吗?拥有迁移的更改检测逻辑可以获取的属性并在scaffolded迁移中生成appropritate方法调用会很好.
我有一个像这样的Generic类:
public class Repository<T> {...}
Run Code Online (Sandbox Code Playgroud)
我需要用字符串来实例...示例:
string _sample = "TypeRepository";
var _rep = new Repository<sample>();
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?这甚至可能吗?
谢谢!
在xUnit中,我可以Theory使用以下形式的泛型:
[Theory]
[MemberData(SomeScenario)]
public void TestMethod<T>(T myType)
{
Assert.Equal(typeof(double), typeof(T));
}
public static IEnumerable<object[]> SomeScenario()
{
yield return new object[] { 1.23D };
}
Run Code Online (Sandbox Code Playgroud)
这将给我通用T参数as double.是否可以使用MemberData为具有以下签名的测试指定泛型类型参数:
[Theory]
[MemberData(SomeTypeScenario)]
public void TestMethod<T>()
{
Assert.Equal(typeof(double), typeof(T));
}
Run Code Online (Sandbox Code Playgroud)
如果无法使用MemberData或任何其他提供的属性(我怀疑它不是),是否可以为Xunit创建一个可以实现此目的的属性?也许类似于在Scenarios方法中指定类型并使用与Jon Skeet的答案类似的方式使用反射:C#中的泛型,使用变量的类型作为参数
我有一个对象列表,其中又包含其他对象的嵌套列表。我想将对象图展平为DataTable.
我找到了接受对象集合并将它们映射到 a DataTable(下面引用)的代码,但它假设属性是可以可靠地转换为字符串值的简单类型。
我认为这只能通过递归来实现,但也许有更好的方法来做到这一点。
想象一下我们有一个ListofCustomer对象:
public class Item
{
public string SKU { get; set; }
public string Description { get; set; }
public double Price { get; set; }
}
public class Order
{
public string ID { get; set; }
public List<Item> Items { get; set; }
}
public class Customer
{
public string Name { get; set; }
public string Email { get; set; }
public List<Order> Orders …Run Code Online (Sandbox Code Playgroud) 我有一个使用通用方法的API如下
public static class DataProvider
{
public static Boolean DeleteDataObject<T>(Guid uid, IDbConnection dbConnection)
{
// Do something here
}
public static IDbConnection GetConnection()
{
// Get connection
}
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序包含在运行时使用CodeDOM生成的类,并且为了跟踪我创建了一个名为的接口IDataObject.我试图将每个对象的具体类型传递给上面的泛型方法,如下所示:
public static Boolean PurgeDataObject(this IDataObject dataObject, Guid uid)
{
return DataProvider.DeleteDataObject<T>(uid, DataProvider.GetConnection());
}
Run Code Online (Sandbox Code Playgroud)
dataObject包含从中继承的类的实例IDataObject.我有兴趣获得该类型并将其传递给T.我试图找出是否有可能在dynamic这里使用.typeof()并且GetType()不像这里所说的那样起作用
我想知道有没有办法将泛型类型作为参数传递给另一个类。换句话说。我有SomeClass<T>和AnotherClass。我想AnotherClass有一个 Type 的实例字段,<T>它会在构造函数中初始化。
(我想SomeClass成为AnotherClass对象列表。另一个类将有 3 个实例字段AnotherClass引用到下一个AnotherClass对象的前一个对象引用和一个T类型字段。
我有一个接受Generic T类的方法
public void CreateTables<T>()
{
string name = typeof(T).Name;
var fields = typeof(T).GetProperties().Select(t => new { key =
t.Name.ToLower(CultureInfo.InvariantCulture), value =
SqLiteUtility.GetSQLiteTypeString(t.PropertyType) })
.ToDictionary(t => t.key, t =>
t.value);
CreateTable(name, fields);
}
and
public void PushData<T>() where T : EntityData
{
var data = _context.Set<T>().Where(p => p.Deleted == false).ToList();
}
Run Code Online (Sandbox Code Playgroud)
我有50多种类型需要调用此方法
CreateTables<Class1>();
PushData<Class1>();
Run Code Online (Sandbox Code Playgroud)
虽然我可以这样做,但我更喜欢创建类型数组并使用for循环来调用此方法
像这样的东西
Type[] types=new Types[]{typeof(Class1),typeof(Class2)}
foreach(var type in types)
{
//call create table method
CreateTables<type>(); - - this doesnt work as "type" is a variable
used …Run Code Online (Sandbox Code Playgroud) 我有以下代码
//^process a aquery and return DataTable
DataTable d t= dbConnect.ProcessQuery(strquery);
Type t = dt.Columns[0].DataType.GetType();
Run Code Online (Sandbox Code Playgroud)
如何创建t将接收的类型的对象dt[0][..]?
我知道这dt.Rows[][]将是类型t
我需要创建一个类型的变量 t
我创建这样的泛型类:
public class Sample<T> where T : class
{
public DoSomething();
}
Run Code Online (Sandbox Code Playgroud)
然后我创建新类:
public class Sample2
{
Sample<Sample2> obj=new Sample<Sample2>();
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能使用下面的代码Sample在Sample2类中创建类的实例?
Sample<typeof<this>> obj=new Sample<typeof<this>>();
Run Code Online (Sandbox Code Playgroud) 我有一个通用的方法
public async Task Save<T>(T aggregate) where T : AggregateRoot
Run Code Online (Sandbox Code Playgroud)
从这个方法我调用另一个泛型方法
var indexRegistrations = IndexRegistrar.GetAll<T>();
Run Code Online (Sandbox Code Playgroud)
现在在第二个通用方法中,我想得到真正的类型T,它是一个子类型AggregateRoot:
public static List<IndexRegistration> GetAll<T>() where T : AggregateRoot
{
return _register.FindAll(r => r.aggregateType == typeof(T));
}
Run Code Online (Sandbox Code Playgroud)
但是,typeof(T)总是返回AggregateRoot.
我怎样才能得到真实的类型(=子类型AggregateRoot)T?
c# ×12
generics ×8
.net ×4
reflection ×3
types ×3
attributes ×1
class ×1
collections ×1
datatable ×1
dynamic ×1
instance ×1
xunit ×1