I have 2 classes as you can see :
static void Main(string[] args)
{
object m = (??????)"salam";
}
public class A
{
public string name { set; get; }
public static implicit operator A(string _name)
{
A aa = new A();
aa.name = _name;
return aa;
}
}
public class B
{
public string family { set; get; }
public static implicit operator B(string _family)
{
B bb = new B();
bb.family = _family;
return bb;
}
}
I need to cast my string in runtime in this line :
object m = (??????)"salam";
Is there any solution to pass my class name as a string to cast my value .for example in runtime I need to cast "salam" to A or maybe B
The static cast is working good like this
object m = (A)salam";
object m = (B)"salam";
But I need to cast my string in runtime
Type x=null;
If(condition)
x can be type of A
else
x can be type of B
object m = (x)"salam";
You need to use Interfaces for such a need. The following code shows how to do so.
To simulate your situtation, I wrote a method to return either A or B based on time.
Here the list contains a bunch of objects which may be of Type A or B, depending on the second of execution. In the real-world scenario, you would get your types in various other ways.
public class StackOverflowQuestion
{
public static void Run()
{
List<IBase> list = new List<IBase>();
string types = "";
for (int i = 0; i < 10; i++)
{
var randomType = GiveMeARandomIBaseType();
System.Threading.Thread.Sleep(750);
IBase hello = randomType.Convert("salam");
list.Add(hello);
types += hello.GetType().Name + ",";
}
types = types.Trim(',');
//sample result : B,B,A,B,A,A,B,A,B,B
}
static IBase GiveMeARandomIBaseType() {
if (DateTime.Now.Second % 2 == 0)
return new A();
else
return new B();
}
}
public interface IBase {
public IBase Convert(string s);
}
public static class MyExtensions {
public static T Convert<T>(this string str, IBase b) where T : IBase {
try
{
return (T)b.Convert(str);
}
catch (Exception)
{
return default;
}
}
}
public class A : IBase
{
public IBase Convert(string s) {
return (A)s;
}
public string name { set; get; }
public static implicit operator A(string _name)
{
A aa = new A();
aa.name = _name;
return aa;
}
}
public class B : IBase
{
public IBase Convert(string s)
{
return (B)s;
}
public string family { set; get; }
public static implicit operator B(string _family)
{
B bb = new B();
bb.family = _family;
return bb;
}
}
I had a similar problem and after all the study and time, I was able to approach the desired result in the following way.
I used an internal method to access (the inside of) the class and this method returns the cast desired result.
Step 1: in class
public class A
{
public string Name { set; get; }
public static implicit operator A(string name)
{
return new A
{
Name = name
};
}
public A GetCasting(object a)
{
A i = (A)a;
return i;
}
}
public class B
{
public string Family { set; get; }
public static implicit operator B(string family)
{
return new B
{
Family = family
};
}
public B GetCasting(object b)
{
B i = (B)b;
return i;
}
}
Step 2: in controller or code
var className = "A";
var classMethod = "GetCasting";
var classType = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsClass && t.Name == className).FirstOrDefault();
var classInstance = Activator.CreateInstance(classType);
var castMethod = classType.GetMethod(classMethod);
var yourObject = "salam";
var objectData = new object[] { yourObject };
var resultObject = castMethod.Invoke(classInstance, objectData);
Related
here is my code:
class Program1
{
private static void Main(string[] args)
{
//MyClass myClass = new MyClass();
//GetIdRequest getIdRequest = new GetIdRequest();
//GetIdReponse getIdReponse= myClass.DoRequest(getIdRequest);
//Console.WriteLine($"getIdReponse.Msg:" + getIdReponse.Msg + ",getIdReponse.Data:" + getIdReponse.Data);
Type myClassType = typeof(MyClass);
Object myClassTypeInstance = System.Activator.CreateInstance(myClassType);
Type unboundGenericType = typeof(MyRequest<>);
Type boundGenericType = unboundGenericType.MakeGenericType(typeof(GetIdRequest));
Type getIdRequestType = typeof(GetIdRequest);
Object getIdRequest = System.Activator.CreateInstance(getIdRequestType);
MethodInfo methodInfo = myClassType.GetMethod("DoRequest", BindingFlags.Public| BindingFlags.Instance, null, new Type[] { boundGenericType }, null);
object res = methodInfo.Invoke(myClassTypeInstance, new object[] { getIdRequest });
Console.ReadKey();
}
}
public class MyClass
{
public T DoRequest<T>(MyRequest<T> obj) where T : MyReponse
{
T res = Activator.CreateInstance<T>();
return res;
}
}
public class GetIdRequest: MyRequest<GetIdReponse>
{
}
public class GetIdReponse: MyReponse
{
public string Data { set; get; } = "data";
}
public interface MyRequest<T> where T: MyReponse
{
}
public class MyReponse
{
public string Msg { set; get; } = "res";
}
i want to Invoke a generic method with generic arguments defined in a generic class by runtime
but i get the Exception
"System.ArgumentException :““ TestPlatformApi.MyRequest `Genericarguments [0] on 1 [t] "“ TestPlatformApi.GetIdRequest ”Violation of constraint on type't'"
As Charlieface commented, you should remove contraint on MyRequest.
From your code, I could not get your design enough. So, I just show some code under suppose.
public class MyClass<T> where T : MsgConstraint
{
private IList<T> middlewares = new List<T>();
public void Register(T m)
{
middlewares.Add(m);
}
public T DoRequest(Type type)
{
return middlewares.Where(st => st.GetType() == type).FirstOrDefault();
}
}
public interface MsgConstraint
{
public string Msg { set; get; }
}
public class HttpMsg : MsgConstraint
{
public string Msg { get; set; } = "HTTPMSG";
}
public class RpcMsg : MsgConstraint
{
public string Msg { get; set; } = "RPCMSG";
}
class Program
{
static void Main(string[] args)
{
MyClass<MsgConstraint> myClass = new MyClass<MsgConstraint>();
myClass.Register(new HttpMsg());
myClass.Register(new RpcMsg());
MsgConstraint http = myClass.DoRequest(typeof(HttpMsg));
Console.WriteLine(http.Msg);
MsgConstraint rpc = myClass.DoRequest(typeof(RpcMsg));
Console.WriteLine(rpc.Msg);
}
}
I know casting is not inherently bad practice, but is it bad in this situation?
I have the following class hierarchy, and the cast at the end.
public class A<T>
{
public A(string name, T value)
{
Name = name;
Value = value;
}
public string Name { get; }
public T Value { get; }
}
public class B : A<double>
{
public B(string name, double value, string expression)
: base(name, value)
{
Expression = expression;
}
public string Expression { get; }
}
public class C
{
public C(A<double> a)
{
_a = a;
}
public string Name { get { return _a.Name; } }
public double Value { get { return _a.Value; } }
protected A<double> _a;
}
public class D : C
{
public D(B b) : base(b)
{
}
public string Expression { get { return ((B)_a).Expression; } }
}
The line in question:
public string Expression { get { return ((B)_a).Expression; } }
Yes this is bad practice; like all downcasting (sometimes necessary to be sure; but that doesn't make it good).
The following code will generate a runtime exception:
A<double> myA = new A<double>("Test", 1.0d);
D test = new D(myA);
var boom = test.Expression; //InvalidCastException
Whereas with a different structure that wouldn't even compile. For example, modifying D to take a B instead of an A<double>
Try this, make the C class generic
public class C<T> where T : A<double>
{
public C(T thing)
{
_thing = thing;
}
protected T _thing;
}
Then D can be an instance of C with a generic argument of B
public D : C<B>
{
Public string Expression { get {return _thing.Expression;}}
}
I'm on my phone so please forgive.sny formatting or typo issues
I would like to build a helper method which will take property as object to anonymous method. This is just dummy code example to visualize problem not confront real solution which is way more complex and is not subject of this question.
Some reference code:
public class FooClass : SomeBaseClass {
public string StringProperty { get; set; }
public int IntProperty { get; set; }
public DateTime DateTimeProperty { get; set; }
public Object ComplexObjectProperty { get; set; }
public FooClass() {
this.FooMethod(this.StringProperty);
this.FooMethod(this.IntProperty);
this.FooMethod(this.DateTimeProperty);
this.FooMethod(this.ComplexObjectProperty);
}
public void FooMethod<T>(T obj) {
Func<bool> validateMethod = () => {
if(obj is string) return string.IsNullOrEmpty(obj.ToString());
return obj != null;
};
this.ValidateMethodsAggregate.Add(validateMethod);
}
}
public class SomeBaseClass {
protected IList<Func<bool>> ValidateMethodsAggregate = new List<Func<bool>>();
public void ValidateAll() {
foreach (var validateMethod in this.ValidateMethodsAggregate) {
var result = validateMethod();
// has errors and so on handling...
}
}
}
// Some simple code to show use case.
var foo = new FooClass();
foo.StringProperty = "new value";
foo.IntProperty = 123;
foo.ValidateAll(); // this will use "" , 0 instead of new values.
Use a function and a conditional operator with a private backing method
public FooClass()
{
this.FooMethod(() => StringProperty); // <- pass an accessor
}
public Func<bool> validateMethod;
private void FooMethod<T>(Func<T> obj)
{
//validate method
validateMethod = () => string.IsNullOrEmpty(obj()?.ToString());
}
The usage would be
var y = new FooClass();
var resTrue = y.validateMethod();
y.StringProperty = "Hi";
var resFalse = y.validateMethod();
Here's a simple class and a derived class:
class A { public int val; }
class B : A { }
I'd like a method Inc which works on both A and B instances that returns a new instance with val incremented by 1.
One way to approach this is to define Inc as an extension method:
static class Extensions
{
public static T Inc<T>(this T obj) where T : A, new()
{
return new T() { val = obj.val + 1 };
}
}
This seems to work. In the following example, a0.val is 11 and b0.val is 21.
var a = new A() { val = 10 };
var b = new B() { val = 20 };
var a0 = a.Inc();
var b0 = b.Inc();
My question is, is there a way to define Inc as a direct member of A and have it work as above?
If I define A like this:
class A
{
public int val;
public T Inc<T>() where T : A, new()
{
return new T() { val = val + 1 };
}
}
I then need to qualify the type when I call Inc:
var a = new A() { val = 10 };
var b = new B() { val = 20 };
var a0 = a.Inc<A>();
var b0 = b.Inc<B>();
Is there a way to go the member method route without having to qualify the type?
I don't think it's possible without implementing a new version of the method on each subclass, i.e,:
class A
{
public int val;
public virtual A Inc()
{
return new A { val = val + 1 };
}
}
class B : A
{
public new B Inc()
{
return new B { val = val + 1 };
}
}
So, you practically want to create clone of object with different value for some field:
class A {
public int val;
protected virtual A CloneInternal() {
return (A)MemberwiseClone();
}
public A Inc() {
A a=CloneInternal();
++a.val;
return a;
}
}
class B:A {
public new B Inc() {
return (B)base.Inc();
}
}
static void Main() {
A a=new B();
a=a.Inc();
Console.WriteLine(a.GetType());
}
Neither I don't think it's possible. You should provide a hint in order to make compiler guesses proper type for T.
You might try with static method:
public static T Inc<T>(T source) where T : A, new()
{
return new T() { val = source.val + 1 };
}
Then,
var b = new B { val = 20 };
var b0 = A.Inc(b);
But it's not an answer since you wanted a member method. I would rather go with extensions method.
abstract class Base
{
public int val { get; set; }
public virtual Base Inc() { return null; }
}
class A : Base
{
public override Base Inc()
{
return new A { val = val + 1 };
}
}
class B : A
{
public override Base Inc()
{
return new B { val = val + 2 };
}
}
Maybe using a abstract base class is better....
Base bClass = new B();
B bInc = bClass.Inc() as B;
One more way
interface IInc
{
int val { get; set; }
IInc GetNew();
}
class A : IInc
{
public int val
{
get;
set;
}
public virtual IInc GetNew()
{
return new A();
}
public IInc Inc()
{
var newObj = GetNew();
newObj.val++;
return newObj;
}
}
class B : A
{
public override IInc GetNew()
{
return new B();
}
}
and use like
var a = new A() { val = 10 };
var b = new B() { val = 20 };
var a0 = a.Inc();
var b0 = b.Inc();
Console.WriteLine(a0.val);
Console.WriteLine(b0.val);
here is a simple example to clear my intentions.
class A {
public int Id
public string Name
public string Hash
public C c
}
class B {
public int id
public string name
public C c
}
class C {
public string name
}
var a = new A() { Id = 123, Name = "something", Hash = "somehash" };
var b = new B();
I want to set b's properties from a. I have tried something but no luck.
public void GenericClassMatcher(object firstModel, object secondModel)
{
if (firstModel != null || secondModel != null)
{
var firstModelType = firstModel.GetType();
var secondModelType = secondModel.GetType();
// to view model
foreach (PropertyInfo prop in firstModelType.GetProperties())
{
var firstModelPropName = prop.Name.ElementAt(0).ToString().ToLower(System.Globalization.CultureInfo.InvariantCulture) + prop.Name.Substring(1); // lowercase first letter
if (prop.PropertyType.FullName.EndsWith("Model"))
{
GenericClassMatcher(prop, secondModelType.GetProperty(firstModelPropName));
}
else
{
var firstModelPropValue = prop.GetValue(firstModel, null);
var secondModelProp = secondModelType.GetProperty(firstModelPropName);
if (prop.PropertyType.Name == "Guid")
{
firstModelPropValue = firstModelPropValue.ToString();
}
secondModelProp.SetValue(secondModel, firstModelPropValue, null);
}
}
}
}
What shall I do?
It sounds like you are trying to map one class to another. AutoMapper is the best tool I've come across to do this.
public class A
{
public int Id;
public string Name;
public string Hash;
public C c;
}
public class B
{
public int id;
public string name;
public C c;
}
public class C
{
public string name;
}
class Program
{
static void Main(string[] args)
{
var a = new A() { Id = 123, Name = "something", Hash = "somehash" };
var b = new B();
AutoMapper.Mapper.CreateMap<A, B>();
b = AutoMapper.Mapper.Map<A, B>(a);
Console.WriteLine(b.id);
Console.WriteLine(b.name);
Console.ReadLine();
}
}