views:

52

answers:

1

I have a reflection method finds all the types in a namespace:

        var models =
            Assembly.GetAssembly(application).GetTypes().Where(
                @t => @t.Namespace == typeof(ViewModelNamespaceBeacon).Namespace).OrderBy(@m => @m.Name).ToList();

My problem is I'm returning an Anonymous type with the name of: {Name = "<>c_DisplayClass2" FullName = "UCHC.CFAR.Web.Models.FieldSecurityViewModel+<>c_DisplayClass2"}

Now from what I've seen detecting anonymous types are difficult( 1141874 ) but I can always filter "funky" names, ie .Contains("<>c_ ") so thats not a big deal.

I'm just trying to find this anonymous type so I can refactor it away. The namespace I'm inspecting is my ViewModel namespace and should be free of too much logic. Now given I've just said that I do have one ViewModel which does perform some logic ( is a mid-refactoring of a couple of other helper classes ) and seems to be identified by name in the name of my anonymous type:

public List<string> Roles { get; private set; }
    public IEnumerable<SelectListItem> ViewModelSelectList { get; private set; }

    public List<SecurityRule> SecurityRules { get; set; }
    public Type SelectedViewModel { get; set; }

    public FieldSecurityViewModel(IEnumerable<string> roles, 
                                  IEnumerable<Type> viewModels, 
                                  string selectedViewModelName = ""
        )
    {
        SetFilteredRoles(roles);

        SetViewModelSelectList(viewModels, selectedViewModelName);
    }

    private void SetViewModelSelectList(IEnumerable<Type> viewModels, string selectedViewModelName)
    {
        ViewModelSelectList = from v in viewModels
                              select new SelectListItem()
                                         {
                                             Selected = v.Name == selectedViewModelName,
                                             Text = GenerateFriendlyViewModelName(v.Name),
                                             Value = v.Name
                                         };
        return;
    }

    private void SetFilteredRoles(IEnumerable<string> roles)
    {
        Roles =  roles.Where(@a => [email protected]("Admin") && [email protected]("NonFacultyUsers") && @a.StartsWith("CFAR.")).ToList();
    }

    public static string GenerateFriendlyViewModelName(string typeName)
    {
        var result = typeName.Replace("ViewModel", "")
            .Replace("GridViewModel", "")
            .Replace("EditViewModel", "")
            .Replace("GridModel", "");

        return result.HumanizeCamel();
    }

    public IEnumerable<ModelMetadata> GetProperties()
    {
        if (SelectedViewModel == null)
            return new List<ModelMetadata>();

        var properties = ModelMetadataProviders.Current.GetMetadataForType(null, SelectedViewModel).Properties;
        return properties.Where(@p => [email protected]("PK_ID") && [email protected]("FK_") && [email protected]("_PK"));
    }

I just can't find the anon type in there.

A: 

A "...DisplayClass" is usually associated with an anonymous method that needs to capture variables.

In this case, I'm betting on the lambda expression associated with the "select new SelectListItem()", since you're referring to the parameter to the method.

Try commenting out the contents of the SetViewModelSelectList method, and see if the anonymous type disappears.

Lasse V. Karlsen