0

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?

11
  • 2
    No need for generics here. You can use method overloading. Two methods with same name but different parameters. Commented May 3, 2016 at 12:01
  • I dont want to create two methods with same code. Commented May 3, 2016 at 12:03
  • Do User and UserReg share a common interface or base class? If not, you won't really benefit from generics here. Commented May 3, 2016 at 12:04
  • When you say same code -- I guess you need an interface / base class. Commented May 3, 2016 at 12:04
  • 1
    Please give us your real code, an not something that, in you opinion, resembles that. Show us PcvCompleteViewModel class and any other class you want to use inside your method. Commented May 3, 2016 at 12:22

4 Answers 4

2

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Do you mean this?

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

Comments

0

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
}

Comments

0

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 );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.