Создание / Изменение Перечислений во Времени выполнения

Самое простое решение - использовать flexbox. Вот рабочая скрипка с использованием библиотеки Tailwind:

The main content

https://jsfiddle.net/nartub/b4fwg3um/

Классы говорят сами за себя, но вы можете обратиться к документам Tailwind, чтобы увидеть, что делает каждый класс.

8
задан torrential coding 31 March 2012 в 07:43
поделиться

4 ответа

Ответ - в простом классе: TypeConverter . (и да, перечисления здесь не подходят).

Поскольку у меня не так много подробностей, я предполагаю, что у вас есть PropertyGrid, "связанный" с целевым экземпляром свойством SelectedObject, и что ваш целевой экземпляр реализует ICustomTypeDescriptor, чтобы вы могли добавлять свойства (то есть PropertyDescriptors) во время выполнения. . Я не знаю вашего дизайна, но если вы не делаете этого, я советую вам взглянуть на него.

Теперь предположим, что вы добавляете свойство строки и хотите, чтобы ваш пользователь мог указать набор ограничения для этого свойства. Ваш пользовательский интерфейс позволяет пользователю вводить набор строк, и в результате вы получаете список строк. Возможно, вы храните словарь свойств в своем целевом экземпляре, поэтому давайте предположим, что этот новый список также хранится там.

Теперь просто напишите новый преобразователь, производный от TypeConverter (или StringConverter, возможно, в этом примере). Вам нужно будет переопределить GetStandardValuesSupported, чтобы вернуть true, и GetStandardValues ​​, чтобы вернуть список строк (используйте параметр context для доступа к свойству Instance и его списку строк). Этот преобразователь будет опубликован вашим PropertyDescriptor со свойством PropertyDescriptor.Converter.

Надеюсь, это не слишком туманно. Если у вас есть конкретный вопрос по этому процессу, просто дайте мне знать.

5
ответ дан 6 December 2019 в 02:26
поделиться

Типичным инженерным решением вашей проблемы является использование списка в качестве справочных данных в вашей базе данных. В общем случае перечисления предназначены для констант, определенных во время компиляции, и их изменение в более поздних выпусках кода не рекомендуется (не говоря уже о времени выполнения), так как это может вызвать побочные эффекты в операторах switch.

3
ответ дан 6 December 2019 в 02:26
поделиться

Вы можете создать код, используя свой код, затем сохранить его во временном текстовом файле, а затем использовать его. Это будет медленно, так как связано с использованием жесткого диска. Я бы порекомендовал рассмотреть отражение .

Редактировать: Я нашел идеальный пример в одной из моих книг, вот он (он довольно длинный, но если вы скопируете его в VS, это будет иметь больше смысла).

namespace Programming_CSharp
{
   using System;
   using System.Diagnostics;
   using System.IO;
   using System.Reflection;
   using System.Reflection.Emit;
   using System.Threading;

   // used to benchmark the looping approach
   public class MyMath
   {
      // sum numbers with a loop
      public int DoSumLooping(int initialVal)
      {
         int result = 0;
         for(int i = 1;i <=initialVal;i++)
         {
            result += i;
         }
         return result;
      }
   }

   // declare the interface
   public interface IComputer
   {
      int ComputeSum(  );
   }

   public class ReflectionTest
   {
      // the private method which emits the assembly
      // using op codes
      private Assembly EmitAssembly(int theValue)
      {
         // Create an assembly name
         AssemblyName assemblyName = 
            new AssemblyName(  );
         assemblyName.Name = "DoSumAssembly";

         // Create a new assembly with one module
         AssemblyBuilder newAssembly =
            Thread.GetDomain(  ).DefineDynamicAssembly(
            assemblyName, AssemblyBuilderAccess.Run);
         ModuleBuilder newModule =
            newAssembly.DefineDynamicModule("Sum");

         //  Define a public class named "BruteForceSums " 
         //  in the assembly.
         TypeBuilder myType =
            newModule.DefineType(
            "BruteForceSums", TypeAttributes.Public);

         // Mark the class as implementing IComputer.
         myType.AddInterfaceImplementation(
            typeof(IComputer));

         // Define a method on the type to call. Pass an
         // array that defines the types of the parameters,
         // the type of the return type, the name of the 
         // method, and the method attributes.
         Type[] paramTypes = new Type[0];
         Type returnType = typeof(int);
         MethodBuilder simpleMethod =
            myType.DefineMethod(
            "ComputeSum",
            MethodAttributes.Public | 
            MethodAttributes.Virtual,
            returnType,
            paramTypes);

         // Get an ILGenerator. This is used
         // to emit the IL that you want.
         ILGenerator generator = 
            simpleMethod.GetILGenerator(  );

         // Emit the IL that you'd get if you 
         // compiled the code example 
         // and then ran ILDasm on the output.

         // Push zero onto the stack. For each 'i' 
         // less than 'theValue', 
         // push 'i' onto the stack as a constant
         // add the two values at the top of the stack.
         // The sum is left on the stack.
         generator.Emit(OpCodes.Ldc_I4, 0);
         for (int i = 1; i <= theValue;i++)
         {
            generator.Emit(OpCodes.Ldc_I4, i);
            generator.Emit(OpCodes.Add);

         }

         // return the value
         generator.Emit(OpCodes.Ret);

         //Encapsulate information about the method and
         //provide access to the method's metadata
         MethodInfo computeSumInfo =
            typeof(IComputer).GetMethod("ComputeSum");

         // specify the method implementation.
         // Pass in the MethodBuilder that was returned 
         // by calling DefineMethod and the methodInfo 
         // just created
         myType.DefineMethodOverride(simpleMethod, computeSumInfo);

         // Create the type.
         myType.CreateType(  );
         return newAssembly;
      }

      // check if the interface is null
      // if so, call Setup.
      public double DoSum(int theValue)
      {
         if (theComputer == null)
         {
            GenerateCode(theValue);
         }

         // call the method through the interface
         return (theComputer.ComputeSum(  ));
      }

      // emit the assembly, create an instance 
      // and get the interface
      public void GenerateCode(int theValue)
      {
         Assembly theAssembly = EmitAssembly(theValue);
         theComputer = (IComputer) 
            theAssembly.CreateInstance("BruteForceSums");
      }

      // private member data
      IComputer theComputer = null;

   }

   public class TestDriver
   {
      public static void Main(  )
      {
         const int val = 2000;  // Note 2,000

         // 1 million iterations!
         const int iterations = 1000000;
         double result = 0;

         // run the benchmark
         MyMath m = new MyMath(  ); 
         DateTime startTime = DateTime.Now;            
         for (int i = 0;i < iterations;i++)
            result = m.DoSumLooping(val);
         }
         TimeSpan elapsed = 
            DateTime.Now - startTime;
         Console.WriteLine(
            "Sum of ({0}) = {1}",val, result);
         Console.WriteLine(
            "Looping. Elapsed milliseconds: " + 
            elapsed.TotalMilliseconds + 
            " for {0} iterations", iterations);

         // run our reflection alternative
         ReflectionTest t = new ReflectionTest(  );

         startTime = DateTime.Now; 
         for (int i = 0;i < iterations;i++)
         {
            result = t.DoSum(val);
         }

         elapsed = DateTime.Now - startTime;
         Console.WriteLine(
            "Sum of ({0}) = {1}",val, result);
         Console.WriteLine(
            "Brute Force. Elapsed milliseconds: " + 
            elapsed.TotalMilliseconds  + 
            " for {0} iterations", iterations);
      }
   }
}

Вывод: Сумма (2000) = 2001000
Циклический. Истекшие миллисекунды:
11468,75 за 1000000 итераций
Сумма (2000) = 2001000
Грубая сила. Истекшие миллисекунды:
406,25 для 1000000 итераций

Здесь - ссылка на всю главу, если вам нужна дополнительная информация.

0
ответ дан 6 December 2019 в 02:26
поделиться

Вы можете использовать Enum.GetNames () и Enum.GetValues ​​() для получения значений и динамического добавления к ним новых. хотя я предлагаю вам использовать список вместо enum или переосмыслить свой дизайн. что-то не пахнет правильно.

-7
ответ дан 6 December 2019 в 02:26
поделиться
Другие вопросы по тегам:

Похожие вопросы: