I want to create a new instance of a object which is holding a list object of another class.
public Class A
{
int a { get; set; };
List<B> b { get; set; }
}
public Class B
{
int c { get; set; };
}
public Class Test
{
A a= new A();
a.b= ? how to initiate this
a.b.c=some value;
}
I am not getting this value c here.how to get This value.
Try it this way:
public Class A
{
public int a { get; set; };
public List<B> b { get; set; }
public A()
{
b = new List<B>();
}
}
public Class B
{
public int c { get; set; };
}
public Class Test
{
A a= new A();
a.b= ? how to initiate this
a.b.Add(new B(){c = 13};
}
1.Try this initializing method
A objA = new A();
objA.a = 10;
objA.b = new List<B> { new B { C = 20 }, new B { C = 40 }, new B { C = 50 }, new B { C = 60 } };
2.Or try this one
A objA = new A();
objA.a = 10;
List<B> bList = new List<B>();
bList.Add(new B { C = 20 });
bList.Add(new B { C = 40 });
bList.Add(new B { C = 50 });
bList.Add(new B {C=60});
objA.b = bList;
And modify access specifiers of properties.
Related
I found this post describing how to conditionally copy values over a destination object if they are not null.
Works great except for list members, it always overwrites them with an empty list. I'm not sure if I've just not configured the mapper correctly or if this is a bug. The following program demonstrates the issue.
namespace automapper_test
{
using AutoMapper;
using System;
using System.Collections.Generic;
class Program
{
class Test
{
public int? A { get; set; }
public string B { get; set; }
public Guid? C { get; set; }
public List<Guid> D { get; set; }
}
static void Main(string[] args)
{
var config = new MapperConfiguration(cfg =>
{
cfg.AllowNullCollections = true;
cfg.CreateMap<Test, Test>().ForAllMembers(opt => opt.Condition((src, dest, member) => member != null));
});
var mapper = config.CreateMapper();
var source = new Test { A = 2, C = Guid.Empty };
var target = new Test { A = 1, B = "hello", C = Guid.NewGuid(), D = new List<Guid> { Guid.NewGuid() } };
mapper.Map(source, target);
System.Diagnostics.Debug.Assert(target.D.Count == 1);
}
}
}
Please let me know below method invocation is thread safe or not.
I am calling ThreadStartMain on my main thread and create new threads and invoke A_GetCounryName method on new instance.
Since i am always calling via new instance i think this is thread safe even though i am having instance variables in some classes.
class MyThread
{
private void ThreadStartMain()
{
for (int i = 0; i < 5; i++)
{
A a = new A();
ThreadStart start = new ThreadStart(a.A_GetCounryName);
Thread t = new Thread(start);
t.Start();
}
}
}
class A
{
public B GetNewObject()
{
B bObj = new B();
return bObj;
}
public void A_GetCounryName()
{
B b=GetObject();
string cName=b.B_GetCoutryName();
}
}
class B
{
C cObj = null;
public B()
{
cObj = new C();
cObj.Prop1 = 1;
cObj.Prop1 = 2;
cObj.Prop1 = 3;
}
public string B_GetCoutryName()
{
string countryName= cObj.C_GetCoutryName();
return countryName;
}
}
class C
{
public int Prop1 { get; set; }
public int Prop2 { get; set; }
public int Prop3 { get; set; }
public string C_GetCoutryName()
{
string name = "Italy";
return name;
}
}
Yes, this is safe because your threads do not share state. More precisely: They do not access common storage locations.
I am using automapper (for .net 3.5). Here is an example to illustrate what I am trying to do:
I want to map an A object to a B object. Class definitions:
class A
{
public I1 MyI { get; set; }
}
class B
{
public I2 MyI { get; set; }
}
interface I1
{
string StringProp1 { get; }
}
interface I2
{
string StringProp1 { get; }
}
class CA : I1
{
public string StringProp1
{
get { return "CA String"; }
}
public string StringProp2 { get; set; }
}
class CB : I2
{
public string StringProp1
{
get { return "CB String"; }
}
public string StringProp2 { get; set; }
}
The mapping code:
A a = new A()
{
MyI = new CA()
};
// Mapper.CreateMap ...?
B b = Mapper.Map<A,B>(a);
I want the resulting object b to be populated with an instance of CB. So automapper needs to know that A maps to B, CA maps to CB, and when creating a B populate it's MyI prop with a CB, how do I specify this mapping?
Something like this:
class Program
{
static void Main(string[] args)
{
Mapper.Initialize(x => x.AddProfile<MappingProfile>());
var a = new A()
{
MyI = new CA()
{
StringProp2 = "sp2"
}
};
var b = Mapper.Map<A, B>(a);
Console.WriteLine("a.MyI.StringProp1: " + a.MyI.StringProp1);
Console.WriteLine("b.MyI.StringProp1: " + b.MyI.StringProp1);
}
}
>= AutoMapper 2.0.0
public class MappingProfile : Profile
{
protected override void Configure()
{
CreateMap<CA, CB>();
CreateMap<CA, I2>().As<CB>();
CreateMap<A, B>();
}
}
AutoMapper 1.1.0.188 (.Net 3.5)
public class MappingProfile : Profile
{
protected override void Configure()
{
CreateMap<CA, CB>();
CreateMap<CA, I2>()
.ConstructUsing(Mapper.Map<CA, CB>)
;
CreateMap<A, B>();
}
}
I have the following scenario . I have three classes
CLass A
Class B
Class C
In Class A an object of Class B is created.
In Class B an object of class C is created.
There is a public class variable defined in Class C
which I want to access using an object of Class A in a page.
Is there any way to do this directly ?
Thanks in advance
Regards
Mathew
You could create a property on A that references the C object:
class A
{
public B B { get; set; }
public int CFoo { get { return B.C.Foo; } set { B.C.Foo = value; } }
public A() { B = new B(); }
}
class B
{
public C C { get; set; }
public B() { C = new C(); }
}
class C
{
public int Foo { get; set; }
}
From your page, you would do this:
A a = new A();
// sets A.B.C.Foo
a.CFoo = 1;
Consider this Example
public class FooWrapper
{
public FooWrapper() { }
public Foo FooObject { get; set; }
public Bar BarObject { get; set; }
}
public IEnumerable<FooWrapper> ListFoosWithBars(int userID)
{
IEnumerable<Bar> tempBar = ListBarsByUserID(userID);
IEnumerable<FooWrapper> results = (
from f in _entities.FooSet
join b in tempBar on f.ID equals b.foos.ID
select new FooWrapper
{
FooObject = f,
BarObject = b
});
return results;
}
what if my Foo type class has Properties like
public class Foo(){
FProperty1{get; set;}
FPorperty2{get; set;}
}
public class Bar(){
BProperty1{get; set;}
BProperty2{get; set;}
}
and now i want to initialize my object in query like this
select new FooWrapper
{
FooObject.FProperty1 = f,
BarObject.BProperty2 = b
});
can I do this?
How will this work?
What you want is:
select new FooWrapper
{
FooObject = new Foo { FProperty1 = f },
BarObject = new Bar { BProperty2 = b }
});