dbobrowski
dbobrowski

Reputation: 846

How do I properly cast this?

How do I properly set the accessBehavior variable?

public sealed class FileAccess : ProjectAccess, IAccess<File>

    interface IAccess<T> where T : ITfsType



        public abstract class Access<T>
        {
            private IAccess<T> accessBehavior;
            public Access()
            {
                FileAccess fa = new FileAccess();
                accessBehavior = //what to assign?
            }
        }

Upvotes: 1

Views: 113

Answers (2)

dbobrowski
dbobrowski

Reputation: 846

FileAccess fa = new FileAccess();
IAccess<T> test = fa as IAccess<T>;

This was my answer. I can now interact with test with the IAccess interface but the concrete type assigned is FileAccess.

Upvotes: 0

CodeNaked
CodeNaked

Reputation: 41393

There isn't a way to cast it based on what you have. FileAcccess implements IAccess<File>, but accessBehavior's type argument is not known. There is no relation between T, which can be any type, and File.

If you had something like:

private IAccess<File> accessBehavior;

Then you could just assign it. Otherwise, you'd need a non-generic base interface, like:

public interface IAccess {
}

public interface IAccess<T> : IAccess {
}


public abstract class Access<T>
{
    private IAccess accessBehavior;

    public Access()
     {
         FileAccess fa = new FileAccess();
         accessBehavior = fa;
     }
 }

But you'd lose the strong typing of the generic type parameter on the members of IAccess.

Upvotes: 2

Related Questions