我在理解如何在现实世界中使用协方差和逆变时遇到了一些麻烦.
到目前为止,我见过的唯一例子是同样的旧数组示例.
object[] objectArray = new string[] { "string 1", "string 2" };
Run Code Online (Sandbox Code Playgroud)
很高兴看到一个允许我在开发过程中使用它的例子,如果我能看到它在其他地方使用的话.
我怎样才能施放List<object>到List<SomethingElse>?
(已知来自哪里SomethingElseobject)
奖金Chatter
铸造清单:
List<Object> first = ...;
List<SomethingElse> second = (List<SomethingElse>)first;
Run Code Online (Sandbox Code Playgroud)
不起作用:
无法将类型'System.Collections.Generic.List'转换为'System.Collections.Generic.List'
铸造清单:
List<SomethingElse> second = first.Cast<SomethingElse>();
Run Code Online (Sandbox Code Playgroud)
不起作用:
不能将类型'System.Collections.Generic.List'隐式转换为'System.Collections.Generic.List'
我实际上并不需要完整的List<T>对象,只是一个ICollection<T>意志:
ICollection<SomethingElse> second = first;
ICollection<SomethingElse> second = (ICollection<SomethingElse>)first;
ICollection<SomethingElse> second = first.Cast<SomethingElse>();
Run Code Online (Sandbox Code Playgroud)
不工作.
我有一个对象列表,这是我的类型QuoteHeader,我想将此列表作为对象列表传递给一个能够接受的方法List<object>.
我的代码行读了......
Tools.MyMethod((List<object>)MyListOfQuoteHeaders);
Run Code Online (Sandbox Code Playgroud)
但是我在设计时遇到以下错误......
Cannot convert type 'System.Collections.Generic.List<MyNameSpace.QuoteHeader>'
to 'System.Collections.Generic.List<object>'
Run Code Online (Sandbox Code Playgroud)
我是否需要对我的班级做任何事情才允许这样做?我以为所有类都继承自对象,所以我无法理解为什么这不起作用?
请在决定投票前重复阅读...
我有一个类型,实现了implicit cast另一种类型的运算符:
class A
{
private B b;
public static implicit operator B(A a) { return a.b; }
}
class B
{
}
Run Code Online (Sandbox Code Playgroud)
现在,隐式和显式转换工作正常:
B b = a;
B b2 = (B)a;
Run Code Online (Sandbox Code Playgroud)
......那么Linq怎么.Cast<>没有?
A[] aa = new A[]{...};
var bb = aa.Cast<B>(); //throws InvalidCastException
Run Code Online (Sandbox Code Playgroud)
看一下源代码.Cast<>,没有太大的魔力:如果参数真的是一个特殊情况IEnumerable<B>,那么:
foreach (object obj in source)
yield return (T)obj;
// ^^ this looks quite similar to the above B b2 = (B)a;
Run Code Online (Sandbox Code Playgroud)
那么为什么我的显式演员会工作,而不是里面的演员.Cast<>? …
给定此类具有隐式转换运算符:
public class MyDateTime
{
public static implicit operator MyDateTime(System.Int64 encoded)
{
return new MyDateTime(encoded);
}
public MyDateTime(System.Int64 encoded)
{
_encoded = encoded;
}
System.Int64 _encoded;
}
Run Code Online (Sandbox Code Playgroud)
我现在可以做以下事情:
long a = 5;
MyDateTime b = a;
Run Code Online (Sandbox Code Playgroud)
但不是以下内容:
long f = 5;
object g = f;
MyDateTime h = g;
Run Code Online (Sandbox Code Playgroud)
这给出了编译时间:
无法将类型'object'隐式转换为'MyDateTime'.
我感觉合理.
现在我修改前面的例子如下:
long f = 5;
object g = f;
MyDateTime h = (MyDateTime)g;
Run Code Online (Sandbox Code Playgroud)
编译好了.现在我得到一个运行时InvalidCastException:
无法将"System.Int64"类型的对象强制转换为"MyDateTime"类型.
这告诉我C#隐式转换运算符仅在编译时应用,并且在.NET运行时试图将对象动态转换为另一种类型时不应用.
我的问题:
顺便说一句,完整的应用程序是我Delegate.DynamicInvoke()用来调用一个带MyDateTime参数的函数,而我传递给的参数的类型很DynamicInvoke长.
c# dynamic-cast type-conversion implicit-cast dynamic-invoke
我有文字.例如string text = "COMPUTER"
,我想将其拆分为字符,以保持每个字符为字符串.
如果有任何分隔符我可以使用text.Split(delimiter).
但是,没有任何分隔符,我将其转换为字符数组用
text.ToCharArray().toList().
然后我得到了List<char>.但我需要List<string>.
那么我该如何转换List<char>为List<string>.
我正在尝试创建一个通用接口,它允许我使用与数据库交互的方法.我希望我的业务应用程序能够实例化任何连接方法,并确保接口是相同的.
这是我现在正在尝试的简化版本.
数据库接口,其中IElement是另一个定义表的接口.
public interface IDatabase
{
void setItem( IElement task ); //this works fine
List<T> listTasks<T>() where T : IElement; // this doesn't
}
Run Code Online (Sandbox Code Playgroud)
IElement界面:
public interface IElement
{
int id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
IElement的实施:
public class TaskElement: IElement
{
public int id { get; set; }
public string name {get; set; }
}
Run Code Online (Sandbox Code Playgroud)
IDatabase的实现:
public class SQLiteDb: IDatabase
{
public SqLiteDb( SQLiteConnection conn )
{
database = conn;
}
public void setItem( IElement task …Run Code Online (Sandbox Code Playgroud) 我需要将一个字符串列表转换为一个对象列表,但问题是我接收这个列表作为一个对象,因为它是一个参数,我不知道它是什么类型.
这是接收参数的函数:
public static bool IsNotEmpty(object obj)
{
if (obj is ICollection)
{
IList<object> collection = (IList<object>)obj; // The cast throws error here
return IsNotEmpty(collection);
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
这就是这个人使用的:
public static bool IsNotEmpty<T>(IList<T> aList)
{
return aList != null && aList.IsNotEmpty();
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能将它作为一个对象列表,然后我可以将它传递给另一个函数?(如果有办法)
c# ×8
generics ×3
casting ×2
covariance ×2
list ×2
c#-4.0 ×1
coercion ×1
dynamic-cast ×1
icollection ×1
ienumerable ×1
interface ×1
linq ×1
split ×1