locked
CodeProperty2.IsDefault behaviour RRS feed

  • Question

  • Hello, I'm working on plugin for VS2010. For it's purposes I need to determine, if CodeProperty2 is default or not. 

    When I try CodeProperty2.IsDefault, it seems that it always return false. But I Expect, that something like that:

    Class TestClass{
     public string TestProperty{get;private set;}
    }

    is default property. Unfortunately, when I try to get body of getter/setter, it throws COMException.

    Is there another way, how to determine if property is default, without catching exceptions ? 

    Thank you very much

    Monday, April 23, 2012 12:09 PM

All replies

  • Hi mirvod,

    Generally speaking, the CodeProperty2.IsDefault function returns true if this is the default property of the class or interface, otherwise false.

    More information:http://msdn.microsoft.com/en-us/library/envdte80.codeproperty2.isdefault.aspx

    It is weird, so please provide some sample code for us to reproduce this issue.

    Thank you for your understanding!


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, April 24, 2012 7:03 AM
  • Hello, here is some sample code. It was run in VsPackage project, only change I made was adding the code below into Tools MenuItemClick handler. (If needed, I can provide whole project)

        private string runTest(DTE dte)
            {
                var results = new StringBuilder();
                foreach(Project prj in dte.Solution.Projects)
                    foreach (ProjectItem itm in prj.ProjectItems)
                    {                    
                        if (itm.FileCodeModel == null) continue;
                        //Traverse all FileCodeModel
                        traverseElements(itm.FileCodeModel.CodeElements,results);
                    }
    
                return results.ToString();
            }
    
            private void traverseElements(CodeElements elements,StringBuilder results)
            {
                foreach (CodeElement el in elements)
                {
                    //run test on codeProperty2
                    if (el is CodeProperty2) results.AppendLine(getResult(el as CodeProperty2));
                    traverseElements(el.Children, results);
                }
            }
    
            private string getResult(CodeProperty2 prop2)
            {
                return string.Format("Property {0}, IsDefault {1}, COMException-IsDefault {2}", prop2.FullName,prop2.IsDefault,COMExTest(prop2));
            }
    
            private bool COMExTest(CodeProperty2 prop2)
            {
                if (prop2.Getter == null || prop2.Setter == null) return false; //default property has to have both get;set;
    
                try
                {
                    // default properties doesnt have body -> this will throw exception
                    var startPoint = prop2.Getter.GetStartPoint(vsCMPart.vsCMPartBody);
                }
                catch (COMException)
                {
                    // probably there is no body, because of default property
                    return true;
                }
                //we recive startpoint-> there is body ->its not default property
                return false;
            }


    when runTest is called in opened solution, travererse all available CodeElements and for each CodeProperty2 try determine if is default or not. It seems, that CodeProperty2.IsDefault always return false.

    When I run test on this sample class:

        class Program
        {
            string PropTest1 { get; set; }
            string PropTest2
            {
                get{return null;}
                set{}
            }
        }


    I get these results:

    Property ConsoleApplication5.Program.PropTest1, IsDefault False, COMException-IsDefault True
    Property ConsoleApplication5.Program.PropTest2, IsDefault False, COMException-IsDefault False

    Any ideas?

    Thank you very much

    Tuesday, April 24, 2012 8:00 AM
  • Hi mirvod,

    Please provide your whole project for us to reproduce this issue.

    You can upload your project to Skydrive or other Network disk. And provide the link. We will try our best to help solve this issue.

    Thank you for your understanding!


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, April 26, 2012 9:09 AM
  • Hello, here is Test solution to download. I think, that SDK Tools are needed for debugging this solution. To test behaviour of CodeProperty2.IsDefault run Test solution and open any project containing class with default property. Then under tools tab run Test on CodeProperty command.

    For me is IsDefault still false. Exception test show right value - but catching exceptions is pretty slow for large project, so I need any better workaround.

    Thank you very much


    • Edited by mirvod Thursday, April 26, 2012 10:34 AM
    Thursday, April 26, 2012 10:32 AM
  • I'm sorry, but I have mismatched default property with automatic property. So my question should be - is there any way, how to determine if CodeProperty remains to automatic property?

    Thank you very much.

    Thursday, May 3, 2012 9:44 PM