如何通过编译器创建随机对象

本文关键字:随机 对象 创建 编译器 何通过 | 更新日期: 2023-09-27 18:35:33

我创建了一个具有 3 个属性的类。该类称为"Testi",在属性之间有一个称为"PercorsoFile"。然后我创建了一个"Testi"列表,每个对象都有属性 3。然后,我创建一个方法,该方法将随机对象作为参数,在所有创建的对象中。这是一个示例(已注释):

private async Task<string> LetturaTestoVeloce (Testi Testoveloce)
        {
            //TestoStorageFile = await StorageFile.GetFileFromPathAsync(Testoveloce[new Random().Next(1,10)].PercorsoFile);
            return await FileIO.ReadTextAsync(TestoStorageFile);            
        }

编译器报告错误我。我不使用括号 [ ]还有其他合适的吗?编辑:错误是:无法将带有[]的索引应用于类型为"Testi"的表达式

如何通过编译器创建随机对象

该错误意味着您无法使用 [] 访问 Testi 类型的某些值。

Testoveloce[..]Testi类型,显然不是任何类型的集合。顺便说一下,有可能通过以下方式启用 [] ,但这取决于您抱怨什么以及您想怎么做。

例如:

public class Testi
{
    /*special indexer property*/
    public string this[int index]
    {
        //return a path based on index
    }
}

这里我使用了C#特定的索引属性