3

I have seen a number of answers for this problem but can't find that how can i resolve the issue I'm facing.

I had the following code vb

     Public Shared Function GetOtherDomains() As List(Of DomainModel)
    Dim list As New List(Of DomainModel)
    Dim items As List(Of Object) = BusinessFactory.tblDomain.GetOtherDomains(Sessions.LoginID)

    For Each item As Object In items
        Dim model As New DomainModel()

        With model
            .LoginID = item.LoginID
            .DomainID = item.CompanyID
            .CompanyName = item.CompanyName
            .RoleName = item.RoleName
        End With

        list.Add(model)
    Next

    Return list
End Function

I converted the code in c# as below and now getting the error('object' does not contain a definition for 'LoginID')

      public static List<DomainModel> GetOtherDomains()
    {
        List<DomainModel> list = new List<DomainModel>();
        List<dynamic> items = BusinessFactory.tblDomain.GetOtherDomains(Sessions.LoginID);

        foreach (dynamic item in items)
        {
            DomainModel model = new DomainModel();

            model.LoginID = item.LoginID;
            model.DomainID = item.CompanyID;
            model.CompanyName = item.CompanyName;
            model.RoleName = item.RoleName;

            list.Add(model);
        }

        return list;
    }

Please let me know that how it can be resolved?

Code Rider
  • 2,003
  • 5
  • 32
  • 50
  • Change `dynamic` to the actual type your `GetOtherDomains()` method returns. –  Feb 26 '15 at 07:59
  • It also returns the same. public List GetOtherDomains(int userID) – Code Rider Feb 26 '15 at 08:01
  • Why are you returning `dynamic`? –  Feb 26 '15 at 08:05
  • The error is telling you that class Object doesn't contains LoginID. You have to cast item to DomainModel to access that member. – LPs Feb 26 '15 at 08:06
  • Could you please clarify which parot of your code is complaining about LoginID? Is it Sessions.LoginID or model.LoginID or item.LoginID? – Alex Sanséau Feb 26 '15 at 14:08
  • This is interesting - 'dynamic' is the corresponding C# equivalent to using 'object' in VB while accessing members that are not of type 'object'. – Dave Doknjas Feb 26 '15 at 15:15
  • 'dynamic' doesn't work well for 'foreach' loop variables - Eric Lippert discusses this in his answer to this post: http://stackoverflow.com/questions/2939024/c-sharp-4-0-dynamic-and-foreach-statement – Dave Doknjas Feb 26 '15 at 15:19

2 Answers2

-1

Is there any specific reason for using dynamic keyword? dynamic keyword is checked at runtime so if there is any compile time error it will skip it. Try using below code:-

  public static List<DomainModel> GetOtherDomains()
    {
        List<DomainModel> list = new List<DomainModel>();
        List<object> items = BusinessFactory.tblDomain.GetOtherDomains(Sessions.LoginID);

        foreach (object item in items)
        {
            DomainModel model = new DomainModel();

            model.LoginID = item.LoginID;
            model.DomainID = item.CompanyID;
            model.CompanyName = item.CompanyName;
            model.RoleName = item.RoleName;

            list.Add(model);
        }

        return list;
    }
-1

It is not simply a case of casting your List<dynamic> as you will get an Unable to cast object of type 'System.Dynamic.ExpandoObject' to type 'DomainModel' error.

If your BusinessFactory returns List<dynamic> items you could take the following approach.

Simply use the JavaScriptSerializer to convert the List<dynamic> into a List<DomainModel>.

This would eleviate even the need to loop or do anything in your method becoming:

   public static List<DomainModel> GetOtherDomains()
   {
        List<dynamic> list = BusinessFactory.tblDomain.GetOtherDomains(Sessions.LoginID);
        var jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        List<DomainModel> domainList = 
                   jsSerializer.ConvertToType<List<DomainModel>>(list);        
        return domainList;        
   }

Here is a simple test setup to prove this works:

void Main()
{
    var list = GetDynamicObjects(); 
    // Linqpad output
    list.Dump();
    var jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    List<DomainModel> domainList = jsSerializer.ConvertToType<List<DomainModel>>(list);
    domainList.Dump();

}


private List<dynamic> GetDynamicObjects()
{
    List<dynamic> list = new List<dynamic>();
    list.Add(GetDynamicObject(1));
    list.Add(GetDynamicObject(2));
    return list;
}

private dynamic GetDynamicObject(int id)
{
    dynamic dyno = new System.Dynamic.ExpandoObject();
    dyno.LoginID  = "LoginID" + id;
    dyno.DomainID  = "DomainID"+ id;
    dyno.CompanyName  = "CompanyName"+ id;
    dyno.RoleName = "RoleName"+ id;
    return dyno;
}

public class DomainModel
{
    public string LoginID { get;set; }
    public string DomainID { get;set; }
    public string CompanyName { get;set; }
    public string RoleName { get;set; }
}

Output

Output

hutchonoid
  • 32,982
  • 15
  • 99
  • 104