我创建了一个对象,并将其分配给在单独的类中找到的两个变量.
如果在一个类中我将变量设置为null,它只会中断对该对象的引用,但由于另一个引用,对象本身仍然存在.
如何将对象设置为null,以便两个引用都指向null?没有找到每一个参考.
这是伪代码.想法是失去ClassA(ref1)和ClassB(ref2)中的引用.
public class ClassA
{
public ClassC ref1 = new ClassC ();
static void Main(string[] args)
{
ref1 = null;
}
}
public class ClassB
{
public static ClassC ref2;
public static AssignC (ClassC c)
{
ref2 = c;
}
}
public class ClassC
{
public ClassC ()
{
ClassB.AssignC (this);
}
}
Run Code Online (Sandbox Code Playgroud) 我知道an Action是a Delegate,但是我在尝试将an分配Action给a 时收到以下编译时错误Delegate。
无法将类型'System.Action'隐式转换为'ADelegate'
public delegate void ADelegate();
Action act = () => Console.WriteLine ("test");
ADelegate del = act;
Run Code Online (Sandbox Code Playgroud)
我该如何分配act给del?
下面的代码不起作用,我想知道如何将实例动态转换为在运行时确定的类型?
Convert.ChangeType()返回仍然需要转换的Object。调用Invoke(),GetConstructor()或Activator.CreateInstance()的所有尝试也是如此,请参见下文。在某个时候,我需要显式地强制转换代码,我希望避免它或将其尽可能地推出。
Type type = Type.GetType ("RandomChildClass");
Object obj = Activator.CreateInstance (type, new Object[]{ "argument" });
var instance = (type)obj;
Run Code Online (Sandbox Code Playgroud)
我知道我可以创建一个接受<T>的方法,但是仍然会有一个不知道如何使用动态随机类型调用它的问题,即 使用Type变量强制转换变量
我正在尝试将我的django模板中的链接添加到由默认管理控制台处理的页面.设置如下,我不相信等式的{%url%}部分是正确的,我收到以下错误:
反向'profile_ProfileModel',找不到参数'()'和关键字参数'{}'.
如何更正此问题并从模板链接到管理控制台.
template.html
<a href="{% url 'admin:profiles_ProfileModel' %}">Profiles</a>
Run Code Online (Sandbox Code Playgroud)
profiles.models.py
class Profile (models.Model)
Run Code Online (Sandbox Code Playgroud)
profiles.admin.py
class ProfileAdmin (admin.ModelAdmin)
admin.site.register (Profile, ProfileAdmin)
Run Code Online (Sandbox Code Playgroud)
url.py
urlpatterns = patterns("", url(r"^admin/", include(admin.site.urls)),)
Run Code Online (Sandbox Code Playgroud) 我想知道最好的方法(非LINQ和LINQ)来测试列表是否包含元素的字段值.
public class A
{
public B bField;
}
public class B {}
List<A> listA = new List<A> ();
B someB = new B();
listA.Contains(someB)
Run Code Online (Sandbox Code Playgroud) 我有一个通用的方法,我想通过引用类型或bool传递.
private void Test<T>(T value)
{
if(typeof(T)) == typeof(bool))
{
if(value == false)
// do something
else if (value == null)
// do something else
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到这个编译时错误"操作员==' cannot be applied to operands of typeT'和`bool'".有一个简单的解决方案(最好不使用反射)?
更新:如果它包含在foreach循环中,是否会有隐式转换?我仍然收到相同的编译错误.
private void Test<T>(T[] values)
{
foreach(T value in values)
{
if(typeof(T)) == typeof(bool))
{
if(value == false)
// do something
}
else if (value == null)
// do something else
}
}
Run Code Online (Sandbox Code Playgroud) 是否有一个简单的lambda表达式从一个列表中提取元素并将它们放入另一个列表中?没有LINQ?
例如,将元素T的源列表映射到另一个列表(或返回列表),其中包含源中每个元素的字符串名称.
使用伪代码更新...
List<int> intList = new List<int>() { 1, 2, 3};
List<string> stringList = new List<string>(intList.ToArray((i) => intList[i].ToString())); // this doesn't work obviously
Run Code Online (Sandbox Code Playgroud)
stringList应为{"1","2","3"}
我正在尝试编写此转换器以适用于所有类型.所以我正在使用泛型.但是收到以下编译时错误.
error CS0411: The type arguments for method 'SomeConverter.ConvertToList<T>(ArrayLike)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Run Code Online (Sandbox Code Playgroud)
是否可以在没有明确说明ConvertToList中的类型的情况下?如果没有,为什么不能在通用方法中推断出T?
using System;
using System.Collections;
using System.Collections.Generic;
public static class SomeConverter
{
public static List<T> ConvertToList<T>(ArrayLike inputArray)
{
List<T> tList = new List<T>();
for(int i=0; i < inputArray.Length; i++)
{
tList.Add ((T)(object)inputArray[i]);
}
return tList;
}
}
public class ArrayLike
{
private string[] arr = new string[100];
public string this[int i]
{
get
{
return arr[i];
}
set
{ …Run Code Online (Sandbox Code Playgroud) 我如何设置以下类,以便我可以有一个方法,它采用一个公共父类/接口,并允许我迭代任何子进程而不指定泛型类型?
public abstract class BaseClass
{ ??? }
public class ChildClass<T> : BaseClass
{
public List<T> SomeList;
???
}
ChildClass<int> childA = new ChildClass<int> ();
ChildClass<string> childB = new ChildClass<string> ();
public void IterateOverChild (BaseClass someChildClass)
{
foreach (var element in someChildClass.SomeList)
{
Console.WriteLine (element);
}
}
Run Code Online (Sandbox Code Playgroud) 在C#中,我如何匹配任何/每个字符(",[,],{,},Az-Zz,0-9, - ,+,*,\,\,/,//,......)在第一个'{'和最后一个'}'之间?
示例字符串: test{abc123-.//"\"\t+*(E WB"T":) {@}[3]$#@}test
结果: {abc123-.//"\"\t+*(E WB"T":) {@}[3]$#@}
删除第一个和最后一个{}之前的所有字符.