Click here to Skip to main content
14,980,646 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
We have a C#/C++ bridge (c# is running on a desktop and the c++ directly on hardware) that is auto generated from special c# files using abstract classes and interfaces to describe the communication bridge to our generator using reflexion.

In trying to remove some of the extra attributes used to do so and make them more c# like I hit a problem.

Is there a way of getting the value of the 'DefaultWindowSize' field using reflection?
public abstract class Averager<InputType>
    //[Static, Value(10)] attributes removed
    public static int DefaultWindowSize = 10;

    public abstract AveragingMode AveragingMode { get; set; }
    public abstract uint NumberOfSamples { get; }

I have tried using:
FieldInfo[] info = currentClass.GetFields(BindingFlags.Static);
object defueltValue = info[0].GetValue(null);

but this gives me a exception:
"Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true."
Updated 28-Aug-11 5:01am
Manfred Rudolf Bihy 26-Aug-11 17:16pm
GetFields returns an array of FieldInfo instances. FieldInfo[] info ... and info[0].GetValue(null) should be correct.
TheRaggers 28-Aug-11 11:03am
Yes sorry that was a typo of me, I corrected it.

1 solution

I've just thrown this small sample together which demonstrates how to fetch the static fields from an abstract generic class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace AbstractGeneric
    class Program
        static void Main(string[] args)
            Type myAbstractGenericType = typeof(AbstractGeneric<>);
            Type[] types = { typeof(int) };
            Type constructed = myAbstractGenericType.MakeGenericType(types);
            FieldInfo[] fieldInfos = constructed.GetFields(
                BindingFlags.NonPublic |
                BindingFlags.Public |
            foreach (FieldInfo fi in fieldInfos)
                Console.WriteLine("Name: {0}  Value: {1}", fi.Name, fi.GetValue(null));
    public abstract class AbstractGeneric<T>
        private static int anInt = 12345;
        private static String aString = "XXYYZZ";
        public abstract T WhatEver { get; set; }

I hope this will help you. If you have any questions regarding this, feel free to add a comment below.

Best Regards,

BillWoodruff 26-Aug-11 23:59pm
+5 An interesting code example, which I've filed away for future reference. Thanks !
TheRaggers 29-Aug-11 7:07am
+5 Working with abstract classes for so long and hitting the wall of "you can't do that because they are abstract" again and again, can make you overlook the simple build-in things in (life) programming.

This really helped me allot. Thanks!
Manfred Rudolf Bihy 29-Aug-11 14:19pm
I'm glad it helped you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900