Преобразуйте StringCollection для списка <представляют в виде строки>

Почему GUI-скриптинг и System Events? Terminal.app является сценарием.

on ButtonTermialClicked_(sender)
     tell application "Terminal"
        activate
        do script "chflags hidden /Users/myself/Desktop/Test" in window 1
     end tell
end ButtonTermianlClicked_

или даже без Terminal.app

on ButtonTermialClicked_(sender)
    do shell script "chflags hidden /Users/myself/Desktop/Test"
end ButtonTermianlClicked_

/Users/myself/Desktop/Test представляет полный путь к папке. Если путь содержит пробелы, вы должны заключить его в одинарные кавычки.

41
задан Community 23 May 2017 в 12:09
поделиться

5 ответов

If you have to use .NET 2.0, I would imagine the cleanliest option would be to create a wrapper for StringCollection that implements IEnumerable and IEnumerator for StringCollection and StringEnumerator respectively. (note: according to metadata, StringEnumerator does not implement IEnumerator). Sample below. However, at the end of the day, someone, somewhere is going to be doing a foreach() over the StringCollection, so one could argue that a simple foreach(string item in stringCollection) and adding to the List would suffice; I doubt this wouldn't be performat enough for your needs.

You could also implement IList using this approach as well, to save you duplicating the underlying strings, but you'll pay a penalty of having the "wrapper" type delegate calls (one more method call on the stack!). I would suggest you treat things in-terms of interfaces within your system anyway IEnumberable, IList etc, instead of the concrete List, it will guide you down a path to greater flexibility.

    static void Main(string[] args)
    {
        StringCollection stringCollection = new StringCollection();
        stringCollection.AddRange(new string[] { "hello", "world" });

        // Wrap!
        List<string> listOfStrings = new List<string>(new StringCollectionEnumerable(stringCollection));

        Debug.Assert(listOfStrings.Count == stringCollection.Count);
        Debug.Assert(listOfStrings[0] == stringCollection[0]); 

    }

    private class StringCollectionEnumerable : IEnumerable<string>
    {
        private StringCollection underlyingCollection; 

        public StringCollectionEnumerable(StringCollection underlyingCollection)
        {
            this.underlyingCollection = underlyingCollection; 
        }

        public IEnumerator<string> GetEnumerator()
        {
            return new StringEnumeratorWrapper(underlyingCollection.GetEnumerator()); 
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator(); 
        }
    }

    private class StringEnumeratorWrapper : IEnumerator<string>
    {
        private StringEnumerator underlyingEnumerator; 

        public StringEnumeratorWrapper(StringEnumerator underlyingEnumerator)
        {
            this.underlyingEnumerator = underlyingEnumerator;
        }

        public string Current
        {
            get
            {
                return this.underlyingEnumerator.Current; 
            }
        }

        public void Dispose()
        {
            // No-op 
        }

        object System.Collections.IEnumerator.Current
        {
            get
            {
                return this.underlyingEnumerator.Current;
            }
        }

        public bool MoveNext()
        {
            return this.underlyingEnumerator.MoveNext(); 
        }

        public void Reset()
        {
            this.underlyingEnumerator.Reset(); 
        }
    }
16
ответ дан 27 November 2019 в 00:07
поделиться

Converting StringCollection to List is wonderfully simple if you're using .NET 3.5.

var list = stringCollection.Cast<string>().ToList();

Regarding your second question, I do seem to recollect that it is possible to use List in the settings designer of Visual Studio. If you select custom type then browser to the appropiate type, it should work. However, I could be mistaken on that, so I'll need to verify it.

104
ответ дан 27 November 2019 в 00:07
поделиться

Почему бы не сделать это простым и просто повторить его и добавить элементы в список, иначе я что-то неправильно понял?

public static List<string> Convert(StringCollection collection)
{
    List<string> list = new List<string>();
    foreach (string item in collection)
    {
        list.Add(item);
    }
    return list;
}
5
ответ дан 27 November 2019 в 00:07
поделиться

Если вы используете C # 3.0 и .NET 3.5, вы можете создать список с помощью

var list = new List<string>(new StringCollection().Cast<string>());

. Другой способ:

var c = new StringCollection();
c.AddRange(new List<string>().ToArray());
3
ответ дан 27 November 2019 в 00:07
поделиться

stringCollection.Cast () .ToList ()

18
ответ дан 27 November 2019 в 00:07
поделиться
Другие вопросы по тегам:

Похожие вопросы: