C#" ??"运营商 - 出了什么问题?

时间:2014-08-20 07:29:12

标签: c# c#-4.0

// EWS Microsoft.Exchange.WebServices.Data.Folder
private Folder _historyFolder;

_historyFolder = GetHistroyFolder(exchangeService) ?? CreateHistortFolder(exchangeService);

public Folder GetHistroyFolder(ExchangeService service)
{
    //if found the folder I want - return it , otherwise returns null
}

public Folder CreateHistortFolder(ExchangeService service)
{
    var folder = new Folder(service);
    folder.DisplayName = _historyFolderName;   // "who cares" :)
    folder.FolderClass = _historyFolderClass; // "IPF.Note"
    folder.Save(WellKnownFolderName.MsgFolderRoot);
    return folder;
}

由于某些原因,_historyFolder始终为null,但GetHistroyFolder(exchangeService)确实会返回一个文件夹。那是为什么?

更新我

我已经更新了不信之人的证据! 如果我将它分成2行(没有??)它就行了!但我还是想明白为什么第一种方法不起作用...... 为什么投票?意思是ppl ..

enter image description here

更新II

感谢所有热情的人投票支持/投票"关闭"这个帖子。

感谢真正热情的人们......

我使用了解决方法,将其拆分为2行

    _historyFolder = GetHistroyFolder(exchangeService);
    if (_historyFolder == null) _historyFolder = CreateHistortFolder(exchangeService); 
你知道有什么好玩的吗? Resharper建议我把它恢复到以前的状态......

是的,这是一个解决方法而不是WHY和WTF的真正答案......(。net 4.5)

更新III

如果GetHistroyFolder(..)返回null(当foreach没有找到...时),CreateHistoryFolder会返回Folder对象,而_historyFolder正在获取值

3 个答案:

答案 0 :(得分:1)

而不是字段为什么不使用具有支持字段的属性。这并没有完全解决问题,但至少这使得调试更容易。

Folder historyFolder 
{
    get{
        if(_historyFolder != null)
            return _historyFolder;
        _historyFolder = GetHistroyFolder(exchangeService); 
        if(_historyFolder == null)
            _historyFolder = CreateHistortFolder(exchangeService) 
        if(_historyFolder == null)
            throw new NullReferenceException("history folder still null");
        return _historyFolder;
    }
}

答案 1 :(得分:1)

如果_historyFolder正在返回某个对象,则GetHistroyFolder()没有理由为NULL。

namespace ConsoleApplication1
{
    class Program
    {
        // EWS Microsoft.Exchange.WebServices.Data.Folder
        private static object _historyFolder;

        static void Main(string[] args)
        {
            _historyFolder = GetHistroyFolder(null) ?? CreateHistortFolder(null);

            Console.WriteLine(_historyFolder == null);
        }

        public static object GetHistroyFolder(object service)
        {
            return new object();
            //if found the folder I want - return it , otherwise returns null
        }

        public static object CreateHistortFolder(object service)
        {
            return null;
        }
    }
}

我只能想象在_historyFolder被调用后GetHistroyFolder()被设置为NULL。您的代码看起来不完整,您是在ASP.NET中运行还是其他什么?

编辑:

在您的代码中,您致电FindFolders(new FolderView())执行此操作:

FindFolders(new FolderView()).ToList()

因为FindFolders返回IEnumerable,我认为您应该调用ToList()以确保一次性返回所有文件夹,而不仅仅是屈服。

答案 2 :(得分:0)

可能是" CreateHistoryFolder" return null