Making a Generic Property

Generic Property in C#

You can make a generic class like this:

public class MyProp<T>
{
private T _value;

public T Value
{
get
{
// insert desired logic here
return _value;
}
set
{
// insert desired logic here
_value = value;
}
}

public static implicit operator T(MyProp<T> value)
{
return value.Value;
}

public static implicit operator MyProp<T>(T value)
{
return new MyProp<T> { Value = value };
}
}

...then use it in a class like so:

class SomeClass
{
public MyProp<int> SomeProperty { get; set; }
}

The implicit operators means that you do not need to explicitly set or get the Value property of MyProp, but can write code to access the value in a more "natural" way:

SomeClass instance = new SomeClass();
instance.SomeProperty = 32;
int someInt = instance.SomeProperty;

How do you create a generic property?

If you set manager via TransactionView constructor then you can do something like this

struct TransactionView<T:Transaction>: View {

@ObservedObject var manager: TransactionManager<T>

var body: some View {
Text("Hello")
}
}

c# Generic property

You need to add <T> to the class and use T as the type of your property.

public class ResponseObject<T>
{
public T data { get; set; }
public Boolean Success { get; set; }
public string Message { get; set; }
}

define a generic property in non-generic base class

There is a way to do this. It uses the ability for all delegates (Func<MyType, bool> is a delegate) to be cast to Delegate.

You'd change IOption and Option to this:

public interface IOption
{
public string Description { get; set; }
Func<T, bool> GetMyFunc<T>();
}

public class Option : IOption
{
string description;
private Delegate expression;

public Option(string description, Delegate expression)
{
this.description = description;
this.expression = expression;
}

public string Description { get; set; }
public Func<T, bool> GetMyFunc<T>() => (Func<T, bool>)this.expression;
}

Then MyClass works as expected (except for the other syntax error in your code).

You then just need to change RunIt on Generic<T> to this:

public override void RunIt()
{
var result = _context.Set<T>().Where(SelectedOption?.GetMyFunc<T>());
// process result
}

How to Create a generic method with 1 known property. Want to dynamic get the properties in different class with Generic Class property

All that you need is to make both your classes implement the same interface, i.e. IDTO:

public interface IDTO
{
string PrimaryIdentifier { get; set; }
}

Then you can tell the compiler to accept only types that implement your new interface:

private void GenericMethod<DTO>(List<DTO> workList, GridView grid, int columnNo)
where DTO: IDTO
{
if (workList.Any())
{
string CodeString = default;

var getData = workList.FirstOrDefault(x => !string.IsNullOrEmpty(x.PrimaryIdentifier));
if (getData != null)
{
CodeString = getData?.PrimaryIdentifier;
}
}
}

(Pay attention to the 2nd row)
Additionally, I also made minor adjustments to your class and method namings based on standard .Net naming convention.

Here's the full code:

    public class Client
{
private void GenericMethod<DTO>(List<DTO> workList, GridView grid, int columnNo)
where DTO: IDTO
{
if (workList.Any())
{
string CodeString = default;

var getData = workList.FirstOrDefault(x => !string.IsNullOrEmpty(x.PrimaryIdentifier));
if (getData != null)
{
CodeString = getData?.PrimaryIdentifier;
}

}

}
}

public class ClassOneDTO : IDTO
{
public int PatientID { get; set; }
public string PrimaryIdentifier { get; set; }
public string FirstName { get; set; }
// so oonnn...
}
public class ClassTwoDTO : IDTO
{
public int EntryID { get; set; }
public string PrimaryIdentifier { get; set; }
public string Location { get; set; }
// so oonnn...
}

public interface IDTO
{
string PrimaryIdentifier { get; set; }
}

EDIT: as Johnathan Barclay correctly pointed out, there's actually no need to have a generic method if you don't need some more advanced logic there that you didn't show in your example.

private void GenericMethod(IEnumerable<IDTO> workList, GridView grid, int columnNo)
{
if (workList.Any())
{
string CodeString = default;

var getData = workList.FirstOrDefault(x => !string.IsNullOrEmpty(x.PrimaryIdentifier));
if (getData != null)
{
CodeString = getData?.PrimaryIdentifier;
}

}
}


Related Topics



Leave a reply



Submit