Help with static methods

Topics: Business Logic Layer
Mar 25, 2011 at 7:52 PM

I have a non static class outside of BlogEngine project. It has some non-static methods.

Example: 

public class NonStatic : INonStatic
{
       public NonStatic();
    
       public List<CustomPost> GetPosts() //CustomPost is the model class that is similar to the Post class for blogengine
       {
             posts= .....// results of linq queries
             return posts;
        }
}

Inside BlogEngine.Core, we have a Post.cs class. In the property "static List<Post> Posts", I want to call the above GetPosts method from the above class which is outside of BlogEngine project. So I added a class " static List<Post> FillPosts()" method inside the Post.cs class where I am trying to instantiate the above nonstatic class as shown below.

Example :

public static List Posts
        {
            get
            {
                new Post();

                if (posts == null)
                {
                    lock (SyncRoot)
                    {
                        if (posts == null)
                        {
                            posts = FillPosts();
                            
                            posts.TrimExcess();
                            AddRelations();
                        }
                    }
                }

                return posts;
            }
        }

 public static List FillPosts()
        {
            var _nonStatic= new NonStatic();

            var pList = _nonStatic.GetPosts();

            if(pList != null)
            {
                var postList = new List();

                foreach(var p in pList)
                {
                    var post = BlogService.SelectPost(p.PostId);
                    postList.Add(post);
                }

                return postList;
            }

            return null;
        }

For some reason, the nonstatic class doesn't get instantiated inside BlogEngine Post.cs class and neither I get any exception after executing the breakpoint in the line marked in red. After the red line is executed, I get some gibberish data in the browser. I am using ASP.NET MVC running on .NET Framework 4.0.  

Please help me. I have spent a lot of time to make it work but nothing worked so far.

Thanks in advance.

 

 

 

 

Mar 28, 2011 at 10:31 PM

Never mind. I found the solution to it. It was throwing a System.TypeAccessException when trying to instantiate an object from an outer non-static class.

 .Net 4.0 has a new security rule that affects the behavior of the AllowSecurityTrustedCallersAttribute attribute(Security-Transparant Code, Level 2). For some reason, my website project assembly didn't have that attribute while BlogEngine.Core had that attribute. I removed that attribute from BlogEngine.Core and everything started working perfectly.