Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建由对象特性引用组成的列表_C#_List_Object_Reference - Fatal编程技术网

C# 创建由对象特性引用组成的列表

C# 创建由对象特性引用组成的列表,c#,list,object,reference,C#,List,Object,Reference,我正在尝试做以下事情: -在第一个方法中,我将遍历一组对象(相同类型),并将指向特定属性的指针提取到列表中 -然后,该列表将在某个时间点被提供给程序中其他地方的另一个方法,并且必须修改原始对象的所有属性(根据提供的列表) 换句话说,假设我们有以下课程: public class Something { public SomeFlag = False; public Something() { } } 在系统的某个地方,我们将一个相关的

我正在尝试做以下事情:
-在第一个方法中,我将遍历一组对象(相同类型),并将指向特定属性的指针提取到列表中
-然后,该列表将在某个时间点被提供给程序中其他地方的另一个方法,并且必须修改原始对象的所有属性(根据提供的列表)

换句话说,假设我们有以下课程:

public class Something  
{  
    public SomeFlag = False;  
    public Something()  
    {  
    }  
}  
在系统的某个地方,我们将一个相关的对象列表组合成一个列表。
现在,我们要扫描这个列表,并将所有标志(通过引用?)提取到“listflags”中:

List flags=新列表();
foreach(列表中的var内容)
{
flags.Add(stuff.SomeFlag);
}
最后,在其他地方,我想更新这些标志,但更新会影响原始对象:

public static void SetStates(List<bool> myList)
{
    // The flag should get set to true by reference in the original object
    myList.SomeFlag = True;
}
公共静态无效集合状态(列表myList)
{
//该标志应通过在原始对象中引用设置为true
myList.SomeFlag=True;
}

使用
操作可以实现以下目的:

public class Something
{
    public bool SomeFlag { get; set; }
}

internal class Program
{
    private static void Main()
    {
        var somethings = new[] {new Something(), new Something()};

        var flags = new List<Action<bool>>();

        // create your list of 'pointers'
        foreach (var something in somethings)
        {
            flags.Add(x => something.SomeFlag = x);
        }

        // set them all to true
        foreach (var action in flags)
        {
            action(true);
        }

        // check the results
        foreach (var something in somethings)
        {
            Console.WriteLine(something.SomeFlag);
        }

        Console.WriteLine("press any key to exit...");
        Console.ReadLine();
    }
}
公共类
{
公共bool SomeFlag{get;set;}
}
内部课程计划
{
私有静态void Main()
{
var Something=new[]{new Something(),new Something()};
var flags=新列表();
//创建“指针”列表
foreach(在something中变为something)
{
flags.Add(x=>something.SomeFlag=x);
}
//把它们都变成真的
foreach(标志中的var操作)
{
行动(真实);
}
//检查结果
foreach(在something中变为something)
{
Console.WriteLine(something.sometflag);
}
Console.WriteLine(“按任意键退出…”);
Console.ReadLine();
}
}

使用
操作可以实现以下目的:

public class Something
{
    public bool SomeFlag { get; set; }
}

internal class Program
{
    private static void Main()
    {
        var somethings = new[] {new Something(), new Something()};

        var flags = new List<Action<bool>>();

        // create your list of 'pointers'
        foreach (var something in somethings)
        {
            flags.Add(x => something.SomeFlag = x);
        }

        // set them all to true
        foreach (var action in flags)
        {
            action(true);
        }

        // check the results
        foreach (var something in somethings)
        {
            Console.WriteLine(something.SomeFlag);
        }

        Console.WriteLine("press any key to exit...");
        Console.ReadLine();
    }
}
公共类
{
公共bool SomeFlag{get;set;}
}
内部课程计划
{
私有静态void Main()
{
var Something=new[]{new Something(),new Something()};
var flags=新列表();
//创建“指针”列表
foreach(在something中变为something)
{
flags.Add(x=>something.SomeFlag=x);
}
//把它们都变成真的
foreach(标志中的var操作)
{
行动(真实);
}
//检查结果
foreach(在something中变为something)
{
Console.WriteLine(something.sometflag);
}
Console.WriteLine(“按任意键退出…”);
Console.ReadLine();
}
}
在C#中,无法保存对属性值的引用(如指向存储该值的内存位置的指针)。只能保存对包含此属性值的对象的引用

var list=new list()
中,可以存储对对象的引用

但是请注意,对于值类型是不可能的。如果
某物
结构
,而不是
,则
列表
将包含对象的副本,而不是对象的引用。因此,我的回答的其余部分假设我们正在谈论
类某事

可以定义特性更改行为并使用对象列表应用它

如果您在编译时已经知道需要哪些属性和值,那么可以创建lambda并传递它

// Define the behavior and get the object list
Action<Something> setter = o => o.Someflag = true;
var objectList = new List<Something>();

// Call your processing method later on
SetProperties(objectList, setter);

void SetProperties<T>(List<T> objects, Action<T> setter)
{
    objects.ForEach(setter);
}
//定义行为并获取对象列表
动作设置器=o=>o.Someflag=true;
var objectList=新列表();
//稍后调用您的处理方法
SetProperties(对象列表、setter);
void SetProperties(列表对象、动作设置器)
{
对象。ForEach(setter);
}
如果您在编译时不知道需要哪些属性和值,那么事情就会变得复杂得多。您需要使用反射来获取属性描述符并设置值

以下是一个简化的示例:

// Define the behavior and get the object list
var objectList = new List<Something>();
string propertyName = "SomeFlag";
PropertyInfo pi = typeof(Something).GetProperty(propertyName);
MethodInfo setter = pi.GetSetMethod();
object value = true;

// Call your processing method later on
SetProperties(objectList, setter, value);

void SetProperties<T>(List<T> objects, MethodInfo setter, object value)
{
    var arguments = new object[] { value } ;
    objects.ForEach(o => setter.Invoke(o, arguments));
}
//定义行为并获取对象列表
var objectList=新列表();
字符串propertyName=“SomeFlag”;
PropertyInfo pi=typeof(Something).GetProperty(propertyName);
MethodInfo setter=pi.GetSetMethod();
对象值=真;
//稍后调用您的处理方法
SetProperties(对象列表、设置器、值);
void SetProperties(列出对象、MethodInfo setter、对象值)
{
变量参数=新对象[]{value};
ForEach(o=>setter.Invoke(o,参数));
}
在C#中,无法保存对属性值的引用(如指向存储该值的内存位置的指针)。只能保存对包含此属性值的对象的引用

var list=new list()
中,可以存储对对象的引用

但是请注意,对于值类型是不可能的。如果
某物
结构
,而不是
,则
列表
将包含对象的副本,而不是对象的引用。因此,我的回答的其余部分假设我们正在谈论
类某事

可以定义特性更改行为并使用对象列表应用它

如果您在编译时已经知道需要哪些属性和值,那么可以创建lambda并传递它

// Define the behavior and get the object list
Action<Something> setter = o => o.Someflag = true;
var objectList = new List<Something>();

// Call your processing method later on
SetProperties(objectList, setter);

void SetProperties<T>(List<T> objects, Action<T> setter)
{
    objects.ForEach(setter);
}
//定义行为并获取对象列表
动作设置器=o=>o.Someflag=true;
var objectList=新列表();
//稍后调用您的处理方法
SetProperties(对象列表、setter);
void SetProperties(列表对象、动作设置器)
{
对象。ForEach(setter);
}
如果您在编译时不知道需要哪些属性和值,那么事情就会变得复杂得多。您需要使用反射来获取属性描述符并设置值

以下是一个简化的示例:

// Define the behavior and get the object list
var objectList = new List<Something>();
string propertyName = "SomeFlag";
PropertyInfo pi = typeof(Something).GetProperty(propertyName);
MethodInfo setter = pi.GetSetMethod();
object value = true;

// Call your processing method later on
SetProperties(objectList, setter, value);

void SetProperties<T>(List<T> objects, MethodInfo setter, object value)
{
    var arguments = new object[] { value } ;
    objects.ForEach(o => setter.Invoke(o, arguments));
}
//定义行为