如何将实际对象传递给采用对象引用的方法

时间:2017-11-02 12:11:26

标签: c++-cli

在C ++ / CLI代码块中,我有这样的代码:

 public class MenuViewModel : MvxViewModel
    {
        private IDataScanner _dataScanner { get; set; }
        private IToastService _toastService { get; set; }

        public MenuViewModel(IDataScanner dataScanner, IToastService toastService)
        {
            _dataScanner = dataScanner;
            _toastService = toastService;
        }

        public override void ViewAppeared()
        {
            base.ViewAppeared();
            RunScan();
        }

        private ObservableCollection<string> _filesCollection;
        public ObservableCollection<string> FilesCollection
        {
            get { return _filesCollection; }
            set
            {
                _filesCollection = value;
               // RaisePropertyChanged(() => FilesCollection);

            }
        }

        private async void RunScan()
        {
            var files = await _dataScanner.GetDataListAsync().ConfigureAwait(false);
            FilesCollection = new ObservableCollection<string>(files);
            //  Someval = files[0];
           _toastService.ShowToast(files[0]);
        }

    }

这个想法是{ System::IO::StreamWriter sw("file"); sw.WriteLine("Hello, world!"); } 会在块结束时自动调用,无论它是如何到达的 - 无论是正常还是抛出异常。

现在,我想将sw.Dispose()传递给C#中定义为sw的方法。这意味着我不能简单地调用

void foo(StreamWriter)

因为foo(sw); 需要foo而我正在传递StreamWriter^。如何将引用传递给StreamWriter

1 个答案:

答案 0 :(得分:1)

您需要使用%运算符!请参阅Tracking References in the MSDN

{
 System::IO::StreamWriter sw("file");
 sw.WriteLine("Hello, world!");
 foo(%sw); 
}