Skip to main content
Removed the unneeded exposition & made the title succinct with the error in the body.
Source Link
ΩmegaMan
  • 32.2k
  • 13
  • 110
  • 138

The type Type must be a reference type in order to use it as parameter 'T' in the generic type or methodType Error When Calling Generic Method

I'm getting deeper into generics and now have a situation I need help with. I get a compile error

The type must be a reference type in order to use it as parameter 'T' in the generic type or method

on the 'Derived' class below as shown in the subject title. I see many other posts similar to this one but I'm not seeing the relationship. Can someone tell me howHow to resolve this?

using System;
using System.Collections.Generic;


namespace Example
{
    public class ViewContext
    {
        ViewContext() { }
    }

    public interface IModel
    {
    }

    public interface IView<T> where T : IModel 
    {
        ViewContext ViewContext { get; set; }
    }

    public class SomeModel : IModel
    {
        public SomeModel() { }
        public int ID { get; set; }
    }

    public class Base<T> where T : IModel
    {

        public Base(IView<T> view)
        {
        }
    }

    public class Derived<SomeModel> : Base<SomeModel> where SomeModel : IModel
    {

        public Derived(IView<SomeModel> view)
            : base(view)
        {
            SomeModel m = (SomeModel)Activator.CreateInstance(typeof(SomeModel));
            Service<SomeModel> s = new Service<SomeModel>();
            s.Work(m);
        }
    }

    public class Service<SomeModel> where SomeModel : IModel
    {
        public Service()
        {
        }

        public void Work(SomeModel m)
        {

        }
    }
}

The type must be a reference type in order to use it as parameter 'T' in the generic type or method

I'm getting deeper into generics and now have a situation I need help with. I get a compile error on the 'Derived' class below as shown in the subject title. I see many other posts similar to this one but I'm not seeing the relationship. Can someone tell me how to resolve this?

using System;
using System.Collections.Generic;


namespace Example
{
    public class ViewContext
    {
        ViewContext() { }
    }

    public interface IModel
    {
    }

    public interface IView<T> where T : IModel 
    {
        ViewContext ViewContext { get; set; }
    }

    public class SomeModel : IModel
    {
        public SomeModel() { }
        public int ID { get; set; }
    }

    public class Base<T> where T : IModel
    {

        public Base(IView<T> view)
        {
        }
    }

    public class Derived<SomeModel> : Base<SomeModel> where SomeModel : IModel
    {

        public Derived(IView<SomeModel> view)
            : base(view)
        {
            SomeModel m = (SomeModel)Activator.CreateInstance(typeof(SomeModel));
            Service<SomeModel> s = new Service<SomeModel>();
            s.Work(m);
        }
    }

    public class Service<SomeModel> where SomeModel : IModel
    {
        public Service()
        {
        }

        public void Work(SomeModel m)
        {

        }
    }
}

Type must be a reference Type Error When Calling Generic Method

I get a compile error

The type must be a reference type in order to use it as parameter 'T' in the generic type or method

on the 'Derived' class below. How to resolve this?

namespace Example
{
    public class ViewContext
    {
        ViewContext() { }
    }

    public interface IModel
    {
    }

    public interface IView<T> where T : IModel 
    {
        ViewContext ViewContext { get; set; }
    }

    public class SomeModel : IModel
    {
        public SomeModel() { }
        public int ID { get; set; }
    }

    public class Base<T> where T : IModel
    {

        public Base(IView<T> view)
        {
        }
    }

    public class Derived<SomeModel> : Base<SomeModel> where SomeModel : IModel
    {

        public Derived(IView<SomeModel> view)
            : base(view)
        {
            SomeModel m = (SomeModel)Activator.CreateInstance(typeof(SomeModel));
            Service<SomeModel> s = new Service<SomeModel>();
            s.Work(m);
        }
    }

    public class Service<SomeModel> where SomeModel : IModel
    {
        public Service()
        {
        }

        public void Work(SomeModel m)
        {

        }
    }
}
don't edit answers into the question, and don't edit in additional questions.
Source Link
Servy
  • 203.8k
  • 27
  • 347
  • 470

UPDATED CODE (THANKS MARK)

using System;
using System.Collections.Generic;


namespace Example
{
    public class ViewContext
    {
        ViewContext() { }
    }

    public interface IModel
    {
    }

    public interface IView<T> where T : class, IModel 
    {
        ViewContext ViewContext { get; set; }
    }

    public class SomeModel : IModel
    {
        public SomeModel() { }
        public int ID { get; set; }
    }

    public class Base<T> where T : class, IModel
    {

        public Base(IView<T> view)
        {
        }
    }

    public class Derived<SomeModel> : Base<SomeModel> where SomeModel : class, IModel
    {

        public Derived(IView<SomeModel> view)
            : base(view)
        {
            SomeModel m = (SomeModel)Activator.CreateInstance(typeof(SomeModel));
            Service<SomeModel> s = new Service<SomeModel>();
            s.Work(m);
        }
    }

    public class Service<SomeModel> where SomeModel : class, IModel
    {
        public Service()
        {
        }

        public void Work(SomeModel m) 
        {

        }
    }
}

A related question I have though is concerning the known bug in 2.0 RE: http://support.microsoft.com/kb/940164 Where a "BadImageFormat" exception is thrown. My code above is in a 3.5 assembly that will be referenced by a 2.0 web site (not web app). Does anyone know if this exception will render it's ugly head in this scenario?

Thanks.

UPDATED CODE (THANKS MARK)

using System;
using System.Collections.Generic;


namespace Example
{
    public class ViewContext
    {
        ViewContext() { }
    }

    public interface IModel
    {
    }

    public interface IView<T> where T : class, IModel 
    {
        ViewContext ViewContext { get; set; }
    }

    public class SomeModel : IModel
    {
        public SomeModel() { }
        public int ID { get; set; }
    }

    public class Base<T> where T : class, IModel
    {

        public Base(IView<T> view)
        {
        }
    }

    public class Derived<SomeModel> : Base<SomeModel> where SomeModel : class, IModel
    {

        public Derived(IView<SomeModel> view)
            : base(view)
        {
            SomeModel m = (SomeModel)Activator.CreateInstance(typeof(SomeModel));
            Service<SomeModel> s = new Service<SomeModel>();
            s.Work(m);
        }
    }

    public class Service<SomeModel> where SomeModel : class, IModel
    {
        public Service()
        {
        }

        public void Work(SomeModel m) 
        {

        }
    }
}

A related question I have though is concerning the known bug in 2.0 RE: http://support.microsoft.com/kb/940164 Where a "BadImageFormat" exception is thrown. My code above is in a 3.5 assembly that will be referenced by a 2.0 web site (not web app). Does anyone know if this exception will render it's ugly head in this scenario?

Thanks.

// UPDATED CODEUPDATED CODE (THANKS MARK)

using System; using System.Collections.Generic;

namespace Example { public class ViewContext { ViewContext() { } }

using System;
using System.Collections.Generic;


namespace Example
{
    public class ViewContext
    {
        ViewContext() { }
    }

    public interface IModel
    {
    }

    public interface IView<T> where T : class, IModel 
    {
        ViewContext ViewContext { get; set; }
    }

    public class SomeModel : IModel
    {
        public SomeModel() { }
        public int ID { get; set; }
    }

    public class Base<T> where T : class, IModel
    {

        public Base(IView<T> view)
        {
        }
    }

    public class Derived<SomeModel> : Base<SomeModel> where SomeModel : class, IModel
    {

        public Derived(IView<SomeModel> view)
            : base(view)
        {
            SomeModel m = (SomeModel)Activator.CreateInstance(typeof(SomeModel));
            Service<SomeModel> s = new Service<SomeModel>();
            s.Work(m);
        }
    }

    public class Service<SomeModel> where SomeModel : class, IModel
    {
        public Service()
        {
        }

        public void Work(SomeModel m) 
        { 

        }
    }
}

}

// UPDATED CODE (THANKS MARK)

using System; using System.Collections.Generic;

namespace Example { public class ViewContext { ViewContext() { } }

public interface IModel
{
}

public interface IView<T> where T : class, IModel 
{
    ViewContext ViewContext { get; set; }
}

public class SomeModel : IModel
{
    public SomeModel() { }
    public int ID { get; set; }
}

public class Base<T> where T : class, IModel
{

    public Base(IView<T> view)
    {
    }
}

public class Derived<SomeModel> : Base<SomeModel> where SomeModel : class, IModel
{

    public Derived(IView<SomeModel> view)
        : base(view)
    {
        SomeModel m = (SomeModel)Activator.CreateInstance(typeof(SomeModel));
        Service<SomeModel> s = new Service<SomeModel>();
        s.Work(m);
    }
}

public class Service<SomeModel> where SomeModel : class, IModel
{
    public Service()
    {
    }

    public void Work(SomeModel m) 
    {

    }
}

}

UPDATED CODE (THANKS MARK)

using System;
using System.Collections.Generic;


namespace Example
{
    public class ViewContext
    {
        ViewContext() { }
    }

    public interface IModel
    {
    }

    public interface IView<T> where T : class, IModel 
    {
        ViewContext ViewContext { get; set; }
    }

    public class SomeModel : IModel
    {
        public SomeModel() { }
        public int ID { get; set; }
    }

    public class Base<T> where T : class, IModel
    {

        public Base(IView<T> view)
        {
        }
    }

    public class Derived<SomeModel> : Base<SomeModel> where SomeModel : class, IModel
    {

        public Derived(IView<SomeModel> view)
            : base(view)
        {
            SomeModel m = (SomeModel)Activator.CreateInstance(typeof(SomeModel));
            Service<SomeModel> s = new Service<SomeModel>();
            s.Work(m);
        }
    }

    public class Service<SomeModel> where SomeModel : class, IModel
    {
        public Service()
        {
        }

        public void Work(SomeModel m) 
        { 

        }
    }
}
added 1505 characters in body
Source Link
ChrisS
  • 2.6k
  • 3
  • 18
  • 19
Loading
Added C# tag
Source Link
Matthias
  • 12.3k
  • 4
  • 53
  • 96
Loading
Source Link
ChrisS
  • 2.6k
  • 3
  • 18
  • 19
Loading