Tag: 类型转换

为什么C#中的隐式类型转换失败?

背景: 我们假设我有以下课程: class Wrapped : IDisposable { public Wrapped(T obj) { /* … */ } public static implicit operator Wrapped(T obj) { return new Wrapped(obj); } public void Dispose() { /* … */ } } 如您所见,它为T → Wrapped提供了隐式类型转换运算符。 最后,我希望能够使用这个类如下: interface IX { /* … */ } class X : IX { /* … */ } […]

动态转换为generics类型

在周末开始之​​前,请快点… 我有一个带有以下签名的方法,我需要调用: public interface IObjectProvider { T Get(Predicate condition); } 这将为我提供符合谓词标准的任何来源的T 现在,必须从我所拥有的上下文调用以下内容: //the actual predicate that’s going to be evaluated var predicate = predicateProperty.GetValue(invocation.InvocationTarget, null); //The type that should go into the call as type param Type relationTargetType = relationDefinition.RelatedType; 正如您可能猜到的,编译器不会让我使用predicate变量作为参数。 我需要做的是将此对象转换为谓词,但通用类型参数必须是编译时常量,所以这不起作用。 我已经开始搞乱这个,但到目前为止没有成功: Type genericPredicateType = typeof(Predicate); Type specificPredicateType= genericPredicateType.MakeGenericType(relationTargetType); Convert.ChangeType(predicate, specificPredicateType) 我怎么能把它混在一起呢? 编辑:我认为这是一个与用例无关的问题,但显然我错了。 所以,既然对我所做的事情,我拥有的以及为什么以及诸如此类的事情有这样的大惊小怪,这里有更多的背景信息。 […]

如何在c#中有条件地转换为多个类型

我正在看这个模式的函数: if( obj is SpecificClass1 ) { ((SpecificClass1)obj).SomeMethod1(); } else if( obj is SpecificClass2 ) { ((SpecificClass2)obj).SomeMethod2(); } else if( obj is SpecificClass3 ) { ((SpecificClass3)obj).SomeMethod3(); } 并获得代码分析警告:CA1800不要不必要地投射。 什么是一个很好的代码模式,我可以使用它代替这个代码将是高性能和简洁。 更新 我没有说,但obj是用类型对象声明的。 我原来在这里问了两个问题。 我已经拆了一个(无论如何还没有人回答): 为什么编译器不会将这两个演员优化为一个?

EF从Type中获取运行时记录列表

目的:我需要循环所有记录,如: var records = db.Set().ToList(); 然后循环 foreach (var record in records) { // do something with the record } 但是它必须在运行时没有特定类型,因为我要遍历类型,因此不知道示例“UserAccount”。 只有Type / TypeOf? 在本说明书的底部,我有一个方法loopAllEntities ,我找不到工作方式 我用一些实体创建了一个DbContext。 public class MyEntities : DbContext { public DbSet UserAccounts { get; set;} public DbSet UserRoles { get; set; } public DbSet UserAccountRoles { get; set; } } 定义了一个Type列表来控制输出: public […]

为什么允许从超类到子类的隐式转换?

有人可以告诉我为什么“//编译”的行编译,以及“//不编译”的行为什么不编译? 我不明白为什么A可以隐含地转换为B,而不是相反。 public class SomeClass { static public void Test() { AClass a = new AClass(); BClass b = new BClass(); a = b; // Compiles b = a; // Doesn’t compile } } public class AClass { public void AMethod() { Console.WriteLine(“AMethod”); } } public class BClass : AClass { public void BMethod() { […]

将BYTE数组转换为INT

我有这种代码 static void Main(string[] args) { byte[] array = new byte[2] { 0x00, 0x1f }; Console.WriteLine(BitConverter.ToInt32(array, 0)); } 但它不起作用。 它引发了一个例外: 目标数组不够长,无法复制集合中的所有项目。 检查数组索引和长度。 怎么了?

为什么“as”运算符不在C#中使用隐式转换运算符?

我在C#(虚拟代码)中定义了从/到某个类型的隐式字符串转换: public class MyType { public string Value { get; set; } public static implicit operator MyType(string fromString) { return new MyType { Value = fromString }; } public static implicit operator string(MyType myType) { return myType.Value; } } 在外部库代码的某处, MyType的实例作为object参数传递给方法。 该方法的一部分看起来像这样的东西: private void Foo(object value) { // … code omitted var bar = value […]

CLR如何知道盒装对象的类型?

当值类型被加框时,它被放置在无类型的引用对象中。 那么是什么导致了无效的强制转换exception? long l = 1; object obj = (object)l; double d = (double)obj;

将字符串转换为BASE62

我正在寻找c#代码将字符串转换为BASE62,如下所示: http://www.molengo.com/base62/title/base62-encoder-decoder 我需要用于URL编码的编码和解码方法。

字符串的通用generics类型转换

我的任务是编写一个StringToType()方法,将字符串转换为指定的类型T. 对于基本类型,我使用Convert.ChangeType()方法 对于枚举类型 – Enum.TryParse() 对于所有其他自定义类型,我创建了一个接口“IConvertibleFromString”,其中包含一个方法“FromString()”,将字符串转换为指定的类型。 任何需要从字符串转换的类都必须实现此接口。 但我不喜欢我实现方法StringToType()的方式。 我想使用少于reflection并尽可能确保性能。 请告知如何最好地实施/更改它。 class Program { static bool StringToType(string str, ref T value) { Type typeT = typeof(T); bool isSuccess = false; if (typeT.GetInterface(“IConvertibleFromString”) != null) { return (bool)typeT.GetMethod(“FromString”).Invoke(value, new object[] { str }); } else if (typeT.IsEnum) { MethodInfo methodTryParse = typeT.GetMethod(“TryParse”).MakeGenericMethod(typeT); return (bool)methodTryParse.Invoke(null, new object[] { […]