csharp 反思的例子

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 反思的例子相关的知识,希望对你有一定的参考价值。

  RootClass _root = new RootClass();
  string _DLLPath = @"c:\PMT.Mobile.Entities.dll";
  string _DLLName = "PMT.Mobile.Entities";
  Assembly TestDLL = Assembly.LoadFile(_DLLPath);
  List<string> _classNamesList = new List<string>();
  Randomizer.Seed = new Random(8675309);
  var faker = new Faker();

  //store all classes names that are inside RequestParams namespace
  foreach (Type type in TestDLL.GetExportedTypes())
      if (type.FullName.Contains("RequestParams") && !type.FullName.Contains("Base"))
          _classNamesList.Add(type.FullName.Split('.').LastOrDefault());

  foreach (var item in _classNamesList)
  {
      Type classType = TestDLL.GetType(_DLLName + ".RequestParams." + item);
      //instance is created
      var classInstance = Activator.CreateInstance(classType);
      //populating the instance with random data
      var propertiesList = classInstance.GetType().GetProperties();
      foreach (var property in propertiesList)
      {
          if (property.PropertyType == typeof(string))
          {
              if(property.Name.ToUpper().Contains("DATE"))
                  property.SetValue(classInstance, faker.Date.Timespan().ToString());
              else if (property.Name.ToUpper().Contains("LANGUAGEID"))
                  property.SetValue(classInstance, "tr");
              else if (property.Name.ToUpper().Contains("ID"))
                  property.SetValue(classInstance, faker.Random.Number(0,100).ToString());
              else
                  property.SetValue(classInstance, faker.Random.String2(2, 40));
          }
          //if sth other than string
          else property.SetValue(classInstance, 0);
      }
      //convert it to Json object
      string JSonObj = JsonConvert.SerializeObject(classInstance);
      //encrypt Json object
      _root.LanguageId = "tr-tr";
      _root.ClientId = "1";
      _root.EncrytedData = JSonObj;
     string stringRoot =  JsonConvert.SerializeObject(_root);
      Console.WriteLine(item + " ----> " + stringRoot + "\n");
      //SendMessage("test", "test", stringRoot);
  }
  Console.ReadKey();

以上是关于csharp 反思的例子的主要内容,如果未能解决你的问题,请参考以下文章

csharp 反思prorperty类型和价值

csharp 反思和习惯[属性]

golang Go的反思例子

QN 笔试反思 HL笔试 反思

csharp Monad C#的例子

csharp Monad C#的例子