我处于这样一种情况,我想要实例化一个将在运行时确定的类型的对象.我还需要对该类型执行显式转换.
像这样的东西:
static void castTest(myEnum val)
{
//Call a native function that returns a pointer to a structure
IntPtr = someNativeFunction(..params..);
//determine the type of the structure based on the enum value
Type structType = getTypeFromEnum(val);
structType myStruct = (structType)Marshal.PtrToStructure(IntPtr, structType);
}
Run Code Online (Sandbox Code Playgroud)
这显然不是有效的代码,但我希望它传达了我想要做的事情的本质.我实际工作的方法必须在~35种不同的类型上执行编组操作.我有几个其他方法需要使用相同的类型集来做类似的事情.所以,我想从这些方法中分离出类型决定逻辑,这样我只需要编写一次,这样方法就可以保持清晰和可读.
我必须承认自己是设计界的新手.有谁能建议一个很好的方法解决这个问题?我怀疑可能有一个我不知道的合适的设计模式.
我正在尝试将某种类型的对象强制转换为它使用的接口Convert.ChangeType(),但是InvalidCastException由于该对象必须实现IConvertible,所以会抛出一个对象.
类型:
public IDocumentSet : IQueryable {}
public IDocumentSet<TDocument> : IDocumentSet, IQueryable<TDocument> {}
public XmlDocumentSet<TDocument> : IDocumentSet<TDocument> {}
Run Code Online (Sandbox Code Playgroud)
从发生错误的代码中摘录:
private readonly ConcurrentDictionary<Type, IDocumentSet> _openDocumentSets = new ConcurrentDictionary<Type, IDocumentSet>();
public void Commit()
{
if (_isDisposed)
throw new ObjectDisposedException(nameof(IDocumentStore));
if (!_openDocumentSets.Any())
return;
foreach (var openDocumentSet in _openDocumentSets)
{
var documentType = openDocumentSet.Key;
var documentSet = openDocumentSet.Value;
var fileName = GetDocumentSetFileName(documentType);
var documentSetPath = Path.Combine(FolderPath, fileName);
using (var stream = new FileStream(documentSetPath, FileMode.Create, …Run Code Online (Sandbox Code Playgroud) 我想实现一个通用的运行时类型转换函数,它使用.Net TypeConverters来进行转换.
有谁知道如何查找和调用特定类型的TypeConverter?
考虑一下这个C#示例:
//
// Convert obj to the type specified by 'toType'.
//
object ConvertTo(object obj, Type toType)
{
if (TypeIsEqualOrDerivesFrom(obj.GetType(), toType)) <-- I know how to implement this.
{
// The type of obj is the same as the type specified by 'toType' or
// the type of obj derives from the type specified by 'toType'.
return obj;
}
if (TypeConverterExists(obj.GetType(), toType) <-- How do I implement this?
{
// There exists a type convertor that is …Run Code Online (Sandbox Code Playgroud) 我有以下类Point和Class2.我的目的是在Class2中检索所有Points实例以将它们存储在List中.
public class Point
{
int x;
int y;
string col;
public Point(int abs, int ord, string clr)
{
this.x = abs;
this.y = ord;
this.col = clr;
}
public string toColor()
{
return this.col;
}
public int Addition()
{
return (this.x + this.y);
}
}
class Class2
{
int test;
Point pt1;
Point pt2;
Point pt3;
List<Point> listPt = new List<Point>() { };
public Class2()
{
test = 100;
this.pt1 = new Point(2, 3, "red");
this.pt2 = new Point(1, …Run Code Online (Sandbox Code Playgroud) 我想知道是否有一种方法可以使用system.reflection转换为精确类型,这样你就可以避免进行显式转换
(System.DateTime)
Run Code Online (Sandbox Code Playgroud)
例如
假设我有一个像这样的词典
Dictionary<propName, Dictionary<object, Type>>
Run Code Online (Sandbox Code Playgroud)
并假设我迭代一个对象道具列表
foreach (var prop in @object.GetType().GetProperties())
{
object propValue = propInfo.GetValue(@object, null);
string propName = propInfo.Name;
Dictionary<object, Type> typeDictionary = new Dictionary<object, Type>();
Type propType = propInfo.GetValue(@object, null).GetType();
typeDictionary[propValue ] = propType ;
propsDictionary[propInfo.Name] = propValue;
}
Run Code Online (Sandbox Code Playgroud)
我想做类似的事情,使用类似的东西投射到精确类型
// this part is only for guidelines
// it should obtain the exact Type
// ... but it returns a string of that type Namespace.Type
Type exactType = Dictionary[enumOfPropName][someValue]
// this part should know the …Run Code Online (Sandbox Code Playgroud)