Click here to Skip to main content
15,891,033 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Another reason I don't like LINQ Pin
Slacker00716-Feb-21 5:20
professionalSlacker00716-Feb-21 5:20 
GeneralRe: Another reason I don't like LINQ Pin
honey the codewitch16-Feb-21 7:02
mvahoney the codewitch16-Feb-21 7:02 
GeneralRe: Another reason I don't like LINQ Pin
Jörgen Andersson16-Feb-21 9:45
professionalJörgen Andersson16-Feb-21 9:45 
GeneralRe: Another reason I don't like LINQ Pin
Greg Utas16-Feb-21 5:25
professionalGreg Utas16-Feb-21 5:25 
GeneralRe: Another reason I don't like LINQ Pin
honey the codewitch16-Feb-21 7:05
mvahoney the codewitch16-Feb-21 7:05 
GeneralRe: Another reason I don't like LINQ Pin
Daniel Pfeffer16-Feb-21 7:17
professionalDaniel Pfeffer16-Feb-21 7:17 
GeneralRe: Another reason I don't like LINQ Pin
Gerry Schmitz16-Feb-21 6:02
mveGerry Schmitz16-Feb-21 6:02 
GeneralRe: Another reason I don't like LINQ Pin
Richard Deeming16-Feb-21 6:05
mveRichard Deeming16-Feb-21 6:05 
My crystal ball says your object's type doesn't contain any generic arguments, so you're passing null to AddRange.
C#
var genericArguments = obj.GetType().GetGenericArguments();
if (genericArguments.Length != 0)
{
    Columns.AddRange(genericArguments[0].GetProperties()
        .Where(p => p.GetCustomAttributes(true).OfType<BrowsableAttribute>().FirstOrDefault()?.Browsable ?? DefaultBrowsableState)
        .Select(p => new ColumnHeader
        {
            Name = p.Name,
            Text = p.GetCustomAttributes(true).OfType<DisplayNameAttribute>().FirstOrDefault()?.DisplayName ?? p.Name,
        });
}
Throw in a custom extension method to simplify it slightly:
C#
public static class AttributeExtensions
{
    public static TAttribute GetCustomAttribute<TAttribute>(this ICustomAttributeProvider value, bool inherit = true)
        where TAttribute : Attribute
    {
        if (value is null) throw new ArgumentNullException(nameof(value));
        
        object[] attributes = value.GetCustomAttributes(typeof(TAttribute), inherit);
        return attributes.Length == 0 ? null : (TAttribute)attributes[0];
    }
}
C#
var genericArguments = obj.GetType().GetGenericArguments();
if (genericArguments.Length != 0)
{
    Columns.AddRange(genericArguments[0].GetProperties()
        .Where(p => p.GetCustomAttribute<BrowsableAttribute>()?.Browsable ?? DefaultBrowsableState)
        .Select(p => new ColumnHeader
        {
            Name = p.Name,
            Text = p.GetCustomAttribute<DisplayNameAttribute>()?.DisplayName ?? p.Name,
        });
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: Another reason I don't like LINQ Pin
honey the codewitch16-Feb-21 6:53
mvahoney the codewitch16-Feb-21 6:53 
GeneralRe: Another reason I don't like LINQ Pin
Sander Rossel16-Feb-21 10:26
professionalSander Rossel16-Feb-21 10:26 
GeneralRe: Another reason I don't like LINQ Pin
musefan16-Feb-21 6:06
musefan16-Feb-21 6:06 
GeneralRe: Another reason I don't like LINQ Pin
obermd16-Feb-21 6:13
obermd16-Feb-21 6:13 
GeneralRe: Another reason I don't like LINQ Pin
Gerry Schmitz16-Feb-21 6:35
mveGerry Schmitz16-Feb-21 6:35 
GeneralRe: Another reason I don't like LINQ Pin
Marc Clifton16-Feb-21 11:13
mvaMarc Clifton16-Feb-21 11:13 
GeneralRe: Another reason I don't like LINQ Pin
Matthew Dennis16-Feb-21 7:41
sysadminMatthew Dennis16-Feb-21 7:41 
GeneralRe: Another reason I don't like LINQ Pin
honey the codewitch16-Feb-21 7:48
mvahoney the codewitch16-Feb-21 7:48 
GeneralRe: Another reason I don't like LINQ Pin
Slacker00716-Feb-21 9:06
professionalSlacker00716-Feb-21 9:06 
GeneralRe: Another reason I don't like LINQ Pin
Richard Deeming16-Feb-21 22:28
mveRichard Deeming16-Feb-21 22:28 
GeneralRe: Another reason I don't like LINQ Pin
Matthew Dennis16-Feb-21 23:04
sysadminMatthew Dennis16-Feb-21 23:04 
GeneralRe: Another reason I don't like LINQ Pin
Richard Deeming16-Feb-21 23:28
mveRichard Deeming16-Feb-21 23:28 
GeneralRe: Another reason I don't like LINQ Pin
Matthew Dennis17-Feb-21 4:51
sysadminMatthew Dennis17-Feb-21 4:51 
GeneralRe: Another reason I don't like LINQ Pin
Matthew Dennis17-Feb-21 5:43
sysadminMatthew Dennis17-Feb-21 5:43 
QuestionRe: Another reason I don't like LINQ Pin
Maximilien16-Feb-21 7:44
Maximilien16-Feb-21 7:44 
AnswerRe: Another reason I don't like LINQ Pin
honey the codewitch16-Feb-21 7:48
mvahoney the codewitch16-Feb-21 7:48 
GeneralRe: Another reason I don't like LINQ Pin
F-ES Sitecore16-Feb-21 7:58
professionalF-ES Sitecore16-Feb-21 7:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.