Bokambo
Bokambo

Reputation: 4480

Passing Generic Object to Method

I want to pass generic(can be of any type) model/class to a method. How to pass that?

if(NewUser)
    MethodA(User);
else
    MethodA(UserReg);

Let me add some more code :

private void SetRegionDummy<T>(T obj)
{
    foreach (var lookup in obj)
    { 
        // but obj.(obj dot) does not give properties of PcvCompleteViewModel
    }
}

//Call this method
SetRegionDummy(this.PcvCompleteViewModel);

[Serializable]
public class PcvCompleteViewModel : StepViewModelBase
{
    #region Constructor


    #endregion

    #region Properties

    public List<ChargeVolume> ChargeVolumes { get; set; }

    public List<LookUpViewModel> LookUp { get; set; }
    public List<ProductViewModel> Products { get; set; }
    public List<ProductViewModel> PricingProducts { get; set; }
    public List<RegionViewModel> Regions { get; set; }
    public decimal ContractYears { get; set; }      
    public decimal? PropGrowthRate { get; set; }
    public decimal? GnicsGrowthRate { get; set; }
}

The method is the same but how to pass a different object model?

Upvotes: 0

Views: 3310

Answers (4)

Federico Dipuma
Federico Dipuma

Reputation: 18265

Both your classes must at least share an interface, or inherit from a common base class, that declares the property you want to be shared among them or else you will not be able to create a method that uses this property.

In your example, you do not need generics at all. Assuming your classes are declared this way:

public class ClassA : IMyInterface {
    public IEnumerable<LookUpViewModel> LookUp { get; set; }
    public int MyPropertyA { get; set; }
    //other properties
}

public class ClassB : IMyInterface {
    public IEnumerable<LookUpViewModel> LookUp { get; set; }
    public string MyPropertyB { get; set; }
    //other properties
}

With a common interface:

public interface IMyInterface {
    IEnumerable<LookUpViewModel> LookUp { get; set; }
}

You can simply create a method that uses this interface as parameter:

private void SetRegionDummy(IMyInterface obj)
{
    foreach (var lookup in obj.LookUp)
    {
        DoWork(lookup);
    }
}

Upvotes: 2

David Pine
David Pine

Reputation: 24525

Generics in C# can be defined by the calling method when the method signature that looks like the following (details on MSDN):

public void MyMethod<T>(T viewModel) where T : IEnumerable<IViewModel>
{
}

Note, that you can use the where syntax to provide type constraints. Then you can invoke this like so:

{
    var viewModel = GetViewModel();
    MyMethod<PcvCompleteViewModel>(viewModel );
}

Upvotes: 0

AntiTcb
AntiTcb

Reputation: 649

If you want to care about the object type, use Generics.

public void MethodA<T>(T obj) {
    // do stuff
}

If you don't, just change the parameter type to object.

public void MethodA(object obj) {
    // do stuff
}

Upvotes: 0

yesman
yesman

Reputation: 7829

Do you mean this?

public void MyMethod<T>(T genericInput)
{
    //do stuff with input
}

Upvotes: 0

Related Questions